Home | History | Annotate | Line # | Download | only in dev
uda1341.c revision 1.10
      1  1.10     peter /*	$NetBSD: uda1341.c,v 1.10 2006/03/25 15:26:51 peter Exp $	*/
      2   1.1    ichiro 
      3   1.1    ichiro /*-
      4   1.1    ichiro  * Copyright (c) 2001 The NetBSD Foundation, Inc.  All rights reserved.
      5   1.1    ichiro  *
      6   1.1    ichiro  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1    ichiro  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      8   1.1    ichiro  *
      9   1.1    ichiro  * Redistribution and use in source and binary forms, with or without
     10   1.1    ichiro  * modification, are permitted provided that the following conditions
     11   1.1    ichiro  * are met:
     12   1.1    ichiro  * 1. Redistributions of source code must retain the above copyright
     13   1.1    ichiro  *    notice, this list of conditions and the following disclaimer.
     14   1.1    ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    ichiro  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    ichiro  *    documentation and/or other materials provided with the distribution.
     17   1.1    ichiro  * 3. All advertising materials mentioning features or use of this software
     18   1.1    ichiro  *    must display the following acknowledgement:
     19   1.1    ichiro  *	This product includes software developed by the NetBSD
     20   1.1    ichiro  *	Foundation, Inc. and its contributors.
     21   1.1    ichiro  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22   1.1    ichiro  *    contributors may be used to endorse or promote products derived
     23   1.1    ichiro  *    from this software without specific prior written permission.
     24   1.1    ichiro  *
     25   1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26   1.1    ichiro  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1    ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1    ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29   1.1    ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1    ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1    ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1    ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1    ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1    ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1    ichiro  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1    ichiro  */
     37   1.5     lukem 
     38   1.5     lukem #include <sys/cdefs.h>
     39  1.10     peter __KERNEL_RCSID(0, "$NetBSD: uda1341.c,v 1.10 2006/03/25 15:26:51 peter Exp $");
     40   1.1    ichiro 
     41   1.1    ichiro #include <sys/param.h>
     42   1.1    ichiro #include <sys/systm.h>
     43   1.1    ichiro #include <sys/types.h>
     44   1.1    ichiro #include <sys/conf.h>
     45   1.1    ichiro #include <sys/file.h>
     46   1.1    ichiro #include <sys/device.h>
     47   1.1    ichiro #include <sys/kernel.h>
     48   1.1    ichiro #include <sys/kthread.h>
     49   1.1    ichiro #include <sys/malloc.h>
     50   1.1    ichiro 
     51   1.1    ichiro #include <machine/bus.h>
     52   1.1    ichiro 
     53   1.1    ichiro #include <hpcarm/dev/ipaq_saipvar.h>
     54   1.1    ichiro #include <hpcarm/dev/ipaq_gpioreg.h>
     55   1.1    ichiro #include <hpcarm/dev/uda1341.h>
     56  1.10     peter 
     57  1.10     peter #include <arm/sa11x0/sa11x0_gpioreg.h>
     58  1.10     peter #include <arm/sa11x0/sa11x0_sspreg.h>
     59   1.1    ichiro 
     60   1.1    ichiro struct uda1341_softc {
     61   1.1    ichiro 	struct device		sc_dev;
     62   1.1    ichiro 	bus_space_tag_t		sc_iot;
     63   1.1    ichiro 	bus_space_handle_t	sc_ioh;
     64   1.1    ichiro 	struct ipaq_softc	*sc_parent;
     65   1.1    ichiro };
     66   1.1    ichiro 
     67   1.1    ichiro static	int	uda1341_match(struct device *, struct cfdata *, void *);
     68   1.1    ichiro static	void	uda1341_attach(struct device *, struct device *, void *);
     69   1.1    ichiro static	int	uda1341_print(void *, const char *);
     70   1.6  drochner static	int	uda1341_search(struct device *, struct cfdata *,
     71   1.7  drochner 			       const int *, void *);
     72   1.1    ichiro 
     73   1.1    ichiro static	void	uda1341_output_high(struct uda1341_softc *);
     74   1.1    ichiro static	void	uda1341_output_low(struct uda1341_softc *);
     75   1.1    ichiro static	void	uda1341_L3_init(struct uda1341_softc *);
     76   1.1    ichiro static	void	uda1341_init(struct uda1341_softc *);
     77   1.1    ichiro static	void	uda1341_reset(struct uda1341_softc *);
     78   1.1    ichiro static	void	uda1341_reginit(struct uda1341_softc *);
     79   1.1    ichiro 
     80   1.1    ichiro static	int	L3_getbit(struct uda1341_softc *);
     81   1.1    ichiro static	void	L3_sendbit(struct uda1341_softc *, int);
     82   1.9     peter static	uint8_t L3_getbyte(struct uda1341_softc *, int);
     83   1.9     peter static	void	L3_sendbyte(struct uda1341_softc *, uint8_t, int);
     84   1.9     peter static	int	L3_read(struct uda1341_softc *, uint8_t, uint8_t *, int);
     85   1.9     peter static	int	L3_write(struct uda1341_softc *, uint8_t, uint8_t *, int);
     86   1.1    ichiro 
     87   1.4   thorpej CFATTACH_DECL(uda, sizeof(struct uda1341_softc),
     88   1.4   thorpej     uda1341_match, uda1341_attach, NULL, NULL);
     89   1.1    ichiro 
     90   1.1    ichiro /*
     91   1.1    ichiro  * Philips L3 bus support.
     92   1.1    ichiro  * GPIO lines are used for clock, data and mode pins.
     93   1.1    ichiro  */
     94   1.1    ichiro #define L3_DATA		GPIO_H3600_L3_DATA
     95   1.1    ichiro #define L3_MODE		GPIO_H3600_L3_MODE
     96   1.1    ichiro #define L3_CLK		GPIO_H3600_L3_CLK
     97   1.1    ichiro 
     98   1.1    ichiro static struct {
     99   1.9     peter 	uint8_t data0;	/* direct addressing register */
    100   1.1    ichiro } DIRECT_REG = {0};
    101   1.1    ichiro 
    102   1.1    ichiro static struct {
    103   1.9     peter 	uint8_t data0;	/* extended addressing register 1 */
    104   1.9     peter 	uint8_t data1;	/* extended addressing register 2 */
    105   1.1    ichiro } EXTEND_REG = {0, 0};
    106   1.1    ichiro 
    107   1.1    ichiro /*
    108   1.1    ichiro  * register space access macros
    109   1.1    ichiro  */
    110   1.1    ichiro #define GPIO_WRITE(sc, reg, val) \
    111   1.1    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_parent->sc_gpioh, reg, val)
    112   1.1    ichiro #define GPIO_READ(sc, reg) \
    113   1.1    ichiro 	bus_space_read_4(sc->sc_iot, sc->sc_parent->sc_gpioh, reg)
    114   1.1    ichiro #define EGPIO_WRITE(sc) \
    115   1.1    ichiro 	bus_space_write_2(sc->sc_iot, sc->sc_parent->sc_egpioh, \
    116   1.1    ichiro 			  0, sc->sc_parent->ipaq_egpio)
    117   1.1    ichiro #define SSP_WRITE(sc, reg, val) \
    118   1.1    ichiro 	bus_space_write_4(sc->sc_iot, sc->sc_parent->sc_ssph, reg, val)
    119   1.1    ichiro 
    120   1.1    ichiro static int
    121   1.1    ichiro uda1341_match(parent, cf, aux)
    122   1.1    ichiro 	struct device *parent;
    123   1.1    ichiro 	struct cfdata *cf;
    124   1.1    ichiro 	void *aux;
    125   1.1    ichiro {
    126   1.1    ichiro 	return (1);
    127   1.1    ichiro }
    128   1.1    ichiro 
    129   1.1    ichiro static void
    130   1.1    ichiro uda1341_attach(parent, self, aux)
    131   1.1    ichiro 	struct device *parent;
    132   1.1    ichiro 	struct device *self;
    133   1.1    ichiro 	void *aux;
    134   1.1    ichiro {
    135   1.1    ichiro 	struct uda1341_softc *sc = (struct uda1341_softc *)self;
    136   1.1    ichiro 	struct ipaq_softc *psc = (struct ipaq_softc *)parent;
    137   1.1    ichiro 
    138   1.1    ichiro 	printf("\n");
    139   1.1    ichiro 	printf("%s: UDA1341 CODEC\n",  sc->sc_dev.dv_xname);
    140   1.1    ichiro 
    141   1.1    ichiro 	sc->sc_iot = psc->sc_iot;
    142   1.1    ichiro 	sc->sc_ioh = psc->sc_ioh;
    143   1.1    ichiro 	sc->sc_parent = (struct ipaq_softc *)parent;
    144   1.1    ichiro 
    145   1.1    ichiro 	uda1341_L3_init(sc);
    146   1.1    ichiro 	uda1341_init(sc);
    147   1.1    ichiro 
    148   1.1    ichiro 	uda1341_reset(sc);
    149   1.1    ichiro 
    150   1.1    ichiro 	uda1341_reginit(sc);
    151   1.1    ichiro 
    152   1.1    ichiro 
    153   1.1    ichiro 	/*
    154   1.1    ichiro 	 *  Attach each devices
    155   1.1    ichiro 	 */
    156   1.1    ichiro 
    157   1.6  drochner 	config_search_ia(uda1341_search, self, "udaif", NULL);
    158   1.1    ichiro }
    159   1.1    ichiro 
    160   1.1    ichiro static int
    161   1.6  drochner uda1341_search(parent, cf, ldesc, aux)
    162   1.1    ichiro 	struct device *parent;
    163   1.1    ichiro 	struct cfdata *cf;
    164   1.7  drochner 	const int *ldesc;
    165   1.1    ichiro 	void *aux;
    166   1.1    ichiro {
    167   1.2   thorpej 	if (config_match(parent, cf, NULL) > 0)
    168   1.1    ichiro 		config_attach(parent, cf, NULL, uda1341_print);
    169   1.1    ichiro 	return 0;
    170   1.1    ichiro }
    171   1.1    ichiro 
    172   1.1    ichiro 
    173   1.1    ichiro static int
    174   1.1    ichiro uda1341_print(aux, name)
    175   1.1    ichiro 	void *aux;
    176   1.1    ichiro 	const char *name;
    177   1.1    ichiro {
    178   1.1    ichiro 	return (UNCONF);
    179   1.1    ichiro }
    180   1.1    ichiro 
    181   1.1    ichiro static void
    182   1.1    ichiro uda1341_output_high(sc)
    183   1.1    ichiro 	struct uda1341_softc *sc;
    184   1.1    ichiro {
    185   1.1    ichiro 	int cr;
    186   1.1    ichiro 
    187   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PSR, (L3_DATA | L3_MODE | L3_CLK));
    188   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PDR) | (L3_DATA | L3_MODE | L3_CLK);
    189   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PDR, cr);
    190   1.1    ichiro }
    191   1.1    ichiro 
    192   1.1    ichiro static void
    193   1.1    ichiro uda1341_output_low(sc)
    194   1.1    ichiro 	struct uda1341_softc *sc;
    195   1.1    ichiro {
    196   1.1    ichiro 	int cr;
    197   1.1    ichiro 
    198   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PDR);
    199   1.1    ichiro 	cr &= ~(L3_DATA | L3_MODE | L3_CLK);
    200   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PDR, cr);
    201   1.1    ichiro }
    202   1.1    ichiro 
    203   1.1    ichiro static void
    204   1.1    ichiro uda1341_L3_init(sc)
    205   1.1    ichiro 	struct uda1341_softc *sc;
    206   1.1    ichiro {
    207   1.1    ichiro 	int cr;
    208   1.1    ichiro 
    209   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_AFR);
    210   1.1    ichiro 	cr &= ~(L3_DATA | L3_MODE | L3_CLK);
    211   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_AFR, cr);
    212   1.1    ichiro 
    213   1.1    ichiro 	uda1341_output_low(sc);
    214   1.1    ichiro }
    215   1.1    ichiro 
    216   1.1    ichiro static void
    217   1.1    ichiro uda1341_init(sc)
    218   1.1    ichiro 	struct uda1341_softc *sc;
    219   1.1    ichiro {
    220   1.1    ichiro 	int cr;
    221   1.1    ichiro 
    222   1.1    ichiro 	/* GPIO initialize */
    223   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_AFR);
    224   1.1    ichiro 	cr &= ~(GPIO_ALT_SSP_TXD | GPIO_ALT_SSP_RXD | GPIO_ALT_SSP_SCLK |
    225   1.1    ichiro 		GPIO_ALT_SSP_SFRM);
    226   1.1    ichiro 	cr |= GPIO_ALT_SSP_CLK;
    227   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_AFR, cr);
    228   1.1    ichiro 
    229   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PDR);
    230   1.1    ichiro 	cr &= ~GPIO_ALT_SSP_CLK;
    231   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PDR, cr);
    232   1.1    ichiro 
    233   1.1    ichiro 	/* SSP initialize & enable */
    234   1.1    ichiro 	SSP_WRITE(sc, SASSP_CR1, CR1_ECS);
    235   1.1    ichiro 	cr = 0xF | (CR0_FRF_MASK & (1<<4)) | (CR0_SCR_MASK & (3<<8)) | CR0_SSE;
    236   1.1    ichiro 	SSP_WRITE(sc, SASSP_CR0, cr);
    237   1.1    ichiro 
    238   1.1    ichiro 	/* Enable the audio power */
    239   1.1    ichiro 	sc->sc_parent->ipaq_egpio |=
    240   1.1    ichiro 			(EGPIO_H3600_AUD_PWRON | EGPIO_H3600_AUD_ON);
    241   1.1    ichiro 	sc->sc_parent->ipaq_egpio &=
    242   1.1    ichiro 			~(EGPIO_H3600_CODEC_RESET | EGPIO_H3600_QMUTE);
    243   1.1    ichiro 	EGPIO_WRITE(sc);
    244   1.1    ichiro 
    245   1.1    ichiro 	/* external clock configured for 44100 samples/sec */
    246   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PDR);
    247   1.1    ichiro 	cr |= (GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1);
    248   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PDR, cr);
    249   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PSR, GPIO_H3600_CLK_SET0);
    250   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PCR, GPIO_H3600_CLK_SET1);
    251   1.1    ichiro 
    252   1.1    ichiro 	/* wait for power on */
    253   1.1    ichiro 	delay(100*1000);
    254   1.1    ichiro 	sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CODEC_RESET;
    255   1.1    ichiro 	EGPIO_WRITE(sc);
    256   1.1    ichiro 
    257   1.1    ichiro 	/* Wait for the UDA1341 to wake up */
    258   1.1    ichiro 	delay(100*1000);
    259   1.1    ichiro }
    260   1.1    ichiro 
    261   1.1    ichiro static void
    262   1.1    ichiro uda1341_reset(sc)
    263   1.1    ichiro 	struct uda1341_softc *sc;
    264   1.1    ichiro {
    265   1.9     peter 	uint8_t command;
    266   1.1    ichiro 
    267   1.1    ichiro 	command = (L3_ADDRESS_COM << 2) | L3_ADDRESS_STATUS;
    268   1.1    ichiro 	DIRECT_REG.data0 = STATUS0_RST | STATUS0_SC_256 | STATUS0_IF_LSB16;
    269   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    270   1.1    ichiro 
    271   1.1    ichiro 	sc->sc_parent->ipaq_egpio &= ~EGPIO_H3600_CODEC_RESET;
    272   1.1    ichiro 	EGPIO_WRITE(sc);
    273   1.1    ichiro 	sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CODEC_RESET;
    274   1.1    ichiro 	EGPIO_WRITE(sc);
    275   1.1    ichiro 
    276   1.1    ichiro 	DIRECT_REG.data0 &= ~STATUS0_RST;
    277   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    278   1.1    ichiro }
    279   1.1    ichiro 
    280   1.1    ichiro static void
    281   1.1    ichiro uda1341_reginit(sc)
    282   1.1    ichiro 	struct uda1341_softc *sc;
    283   1.1    ichiro {
    284   1.9     peter 	uint8_t command;
    285   1.1    ichiro 
    286   1.1    ichiro 	/* STATUS 0 */
    287   1.1    ichiro 	command = (L3_ADDRESS_COM << 2) | L3_ADDRESS_STATUS;
    288   1.1    ichiro 	DIRECT_REG.data0 = STATUS0_SC_256 | STATUS0_IF_LSB16;
    289   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    290   1.1    ichiro 
    291   1.1    ichiro 	/* STATUS 1 */
    292   1.1    ichiro 	DIRECT_REG.data0 = STATUS1_OGS | STATUS1_IGS | (1<<7);
    293   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    294   1.1    ichiro 
    295   1.1    ichiro 	/* DATA 0 */
    296   1.1    ichiro 	command = (L3_ADDRESS_COM << 2) | L3_ADDRESS_DATA0;
    297   1.1    ichiro 	DIRECT_REG.data0 = DATA0_VC(100) | DATA0_COMMON;
    298   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    299   1.1    ichiro 
    300   1.1    ichiro 	/* DATA 1 */
    301   1.1    ichiro 	DIRECT_REG.data0 = DATA1_BB(0) | DATA1_TR(0) | DATA1_COMMON;
    302   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    303   1.1    ichiro 
    304   1.1    ichiro 	/* DATA 2*/
    305   1.1    ichiro 	DIRECT_REG.data0 = DATA2_PP | DATA2_COMMON;
    306   1.9     peter 	L3_write(sc, command, (uint8_t *) &DIRECT_REG, 1);
    307   1.1    ichiro 
    308   1.1    ichiro 	/* Extended DATA 0 */
    309   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E0;
    310   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN | 0x4 ;
    311   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    312   1.1    ichiro 
    313   1.1    ichiro 	/* Extended DATA 1 */
    314   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E1;
    315   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN | 0x4 ;
    316   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    317   1.1    ichiro 
    318   1.1    ichiro 	/* Extended DATA 2 */
    319   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E2;
    320   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN | DATA_E2_MS(30);
    321   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    322   1.1    ichiro 
    323   1.1    ichiro 	/* Extended DATA 3 */
    324   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E3;
    325   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN | DATA_E3_IG_L(0);
    326   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    327   1.1    ichiro 
    328   1.1    ichiro 	/* Extended DATA 4 */
    329   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E4;
    330   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN | DATA_E4_IG_H(0);
    331   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    332   1.1    ichiro 
    333   1.1    ichiro 	/* Extended DATA 5 */
    334   1.1    ichiro 	EXTEND_REG.data0 = EXT_ADDR_COMMON | EXT_ADDR_E5;
    335   1.1    ichiro 	EXTEND_REG.data1 = EXT_DATA_COMMN;
    336   1.9     peter 	L3_write(sc, command, (uint8_t *) &EXTEND_REG, 2);
    337   1.1    ichiro }
    338   1.1    ichiro 
    339   1.1    ichiro static int
    340   1.1    ichiro L3_getbit(sc)
    341   1.1    ichiro 	struct uda1341_softc *sc;
    342   1.1    ichiro {
    343   1.1    ichiro 	int cr, data;
    344   1.1    ichiro 
    345   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK);	/* Clock down */
    346   1.1    ichiro 	delay(L3_CLK_LOW);
    347   1.1    ichiro 
    348   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PLR);
    349   1.1    ichiro 	data = (cr & L3_DATA) ? 1 : 0;
    350   1.1    ichiro 
    351   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK);	/* Clock up */
    352   1.1    ichiro 	delay(L3_CLK_HIGH);
    353   1.1    ichiro 
    354   1.1    ichiro 	return (data);
    355   1.1    ichiro }
    356   1.1    ichiro 
    357   1.1    ichiro static void
    358   1.1    ichiro L3_sendbit(sc, bit)
    359   1.1    ichiro 	struct uda1341_softc *sc;
    360   1.1    ichiro 	int bit;
    361   1.1    ichiro {
    362   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK);	/* Clock down */
    363   1.1    ichiro 
    364   1.1    ichiro 	if (bit & 0x01)
    365   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PSR, L3_DATA);
    366   1.1    ichiro 	else
    367   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PCR, L3_DATA);
    368   1.1    ichiro 
    369   1.1    ichiro 	delay(L3_CLK_LOW);
    370   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK);     /* Clock up */
    371   1.1    ichiro 	delay(L3_CLK_HIGH);
    372   1.1    ichiro }
    373   1.1    ichiro 
    374   1.9     peter static uint8_t
    375   1.1    ichiro L3_getbyte(sc, mode)
    376   1.1    ichiro 	struct uda1341_softc *sc;
    377   1.1    ichiro 	int mode;
    378   1.1    ichiro {
    379   1.1    ichiro 	int i;
    380   1.9     peter 	uint8_t data;
    381   1.1    ichiro 
    382   1.1    ichiro 	switch (mode) {
    383   1.1    ichiro 	case 0:		/* Address mode */
    384   1.1    ichiro 	case 1:		/* First data byte */
    385   1.1    ichiro 		break;
    386   1.1    ichiro 	default:	/* second data byte via halt-Time */
    387   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK);     /* Clock down */
    388   1.1    ichiro 		delay(L3_HALT);
    389   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK);	/* Clock up */
    390   1.1    ichiro 		break;
    391   1.1    ichiro 	}
    392   1.1    ichiro 
    393   1.1    ichiro 	delay(L3_MODE_SETUP);
    394   1.1    ichiro 
    395   1.1    ichiro 	for (i = 0; i < 8; i++)
    396   1.1    ichiro 		data |= (L3_getbit(sc) << i);
    397   1.1    ichiro 
    398   1.1    ichiro 	delay(L3_MODE_HOLD);
    399   1.1    ichiro 
    400   1.1    ichiro 	return (data);
    401   1.1    ichiro }
    402   1.1    ichiro 
    403   1.1    ichiro static void
    404   1.1    ichiro L3_sendbyte(sc, data, mode)
    405   1.1    ichiro 	struct uda1341_softc *sc;
    406   1.9     peter 	uint8_t data;
    407   1.1    ichiro 	int mode;
    408   1.1    ichiro {
    409   1.1    ichiro 	int i;
    410   1.1    ichiro 
    411   1.1    ichiro 	switch (mode) {
    412   1.1    ichiro 	case 0:		/* Address mode */
    413   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK);	/* Clock down */
    414   1.1    ichiro 		break;
    415   1.1    ichiro 	case 1:		/* First data byte */
    416   1.1    ichiro 		break;
    417   1.1    ichiro 	default:	/* second data byte via halt-Time */
    418   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK);     /* Clock down */
    419   1.1    ichiro 		delay(L3_HALT);
    420   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK);	/* Clock up */
    421   1.1    ichiro 		break;
    422   1.1    ichiro 	}
    423   1.1    ichiro 
    424   1.1    ichiro 	delay(L3_MODE_SETUP);
    425   1.1    ichiro 
    426   1.1    ichiro 	for (i = 0; i < 8; i++)
    427   1.1    ichiro 		L3_sendbit(sc, data >> i);
    428   1.1    ichiro 
    429   1.1    ichiro 	if (mode == 0)		/* Address mode */
    430   1.1    ichiro 		GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK);	/* Clock up */
    431   1.1    ichiro 
    432   1.1    ichiro 	delay(L3_MODE_HOLD);
    433   1.1    ichiro }
    434   1.1    ichiro 
    435   1.1    ichiro static int
    436   1.1    ichiro L3_read(sc, addr, data, len)
    437   1.1    ichiro 	struct uda1341_softc *sc;
    438   1.9     peter 	uint8_t addr, *data;
    439   1.1    ichiro         int len;
    440   1.1    ichiro {
    441   1.1    ichiro 	int cr, mode;
    442   1.1    ichiro 	mode = 0;
    443   1.1    ichiro 
    444   1.1    ichiro 	uda1341_output_high(sc);
    445   1.1    ichiro 	L3_sendbyte(sc, addr, mode++);
    446   1.1    ichiro 
    447   1.1    ichiro 	cr = GPIO_READ(sc, SAGPIO_PDR);
    448   1.1    ichiro 	cr &= ~(L3_DATA);
    449   1.1    ichiro 	GPIO_WRITE(sc, SAGPIO_PDR, cr);
    450   1.1    ichiro 
    451   1.1    ichiro 	while(len--)
    452   1.1    ichiro 		*data++ = L3_getbyte(sc, mode++);
    453   1.1    ichiro 	uda1341_output_low(sc);
    454   1.1    ichiro 
    455   1.1    ichiro 	return len;
    456   1.1    ichiro }
    457   1.1    ichiro 
    458   1.1    ichiro static int
    459   1.1    ichiro L3_write(sc, addr, data, len)
    460   1.1    ichiro 	struct uda1341_softc *sc;
    461   1.9     peter 	uint8_t addr, *data;
    462   1.1    ichiro 	int len;
    463   1.1    ichiro {
    464   1.1    ichiro 	int mode = 0;
    465   1.1    ichiro 
    466   1.1    ichiro 	uda1341_output_high(sc);
    467   1.1    ichiro 	L3_sendbyte(sc, addr, mode++);
    468   1.1    ichiro 	while(len--)
    469   1.1    ichiro 		L3_sendbyte(sc, *data++, mode++);
    470   1.1    ichiro 	uda1341_output_low(sc);
    471   1.1    ichiro 
    472   1.1    ichiro 	return len;
    473   1.1    ichiro }
    474