2 * Low-level device IO routines for ST-Ericsson CW1200 drivers
4 * Copyright (c) 2010, ST-Ericsson
5 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
8 * ST-Ericsson UMAC CW1200 driver, which is
9 * Copyright (c) 2010, ST-Ericsson
10 * Author: Ajitpal Singh <ajitpal.singh@lockless.no>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/types.h>
23 /* Sdio addr is 4*spi_addr */
24 #define SPI_REG_ADDR_TO_SDIO(spi_reg_addr) ((spi_reg_addr) << 2)
25 #define SDIO_ADDR17BIT(buf_id, mpf, rfu, reg_id_ofs) \
26 ((((buf_id) & 0x1F) << 7) \
27 | (((mpf) & 1) << 6) \
28 | (((rfu) & 1) << 5) \
29 | (((reg_id_ofs) & 0x1F) << 0))
33 static int __cw1200_reg_read(struct cw1200_common
*priv
, u16 addr
,
34 void *buf
, size_t buf_len
, int buf_id
)
37 u32 sdio_reg_addr_17bit
;
39 /* Check if buffer is aligned to 4 byte boundary */
40 if (WARN_ON(((unsigned long)buf
& 3) && (buf_len
> 4))) {
41 pr_err("buffer is not aligned.\n");
45 /* Convert to SDIO Register Address */
46 addr_sdio
= SPI_REG_ADDR_TO_SDIO(addr
);
47 sdio_reg_addr_17bit
= SDIO_ADDR17BIT(buf_id
, 0, 0, addr_sdio
);
49 return priv
->hwbus_ops
->hwbus_memcpy_fromio(priv
->hwbus_priv
,
54 static int __cw1200_reg_write(struct cw1200_common
*priv
, u16 addr
,
55 const void *buf
, size_t buf_len
, int buf_id
)
58 u32 sdio_reg_addr_17bit
;
60 /* Convert to SDIO Register Address */
61 addr_sdio
= SPI_REG_ADDR_TO_SDIO(addr
);
62 sdio_reg_addr_17bit
= SDIO_ADDR17BIT(buf_id
, 0, 0, addr_sdio
);
64 return priv
->hwbus_ops
->hwbus_memcpy_toio(priv
->hwbus_priv
,
69 static inline int __cw1200_reg_read_32(struct cw1200_common
*priv
,
73 int i
= __cw1200_reg_read(priv
, addr
, &tmp
, sizeof(tmp
), 0);
74 *val
= le32_to_cpu(tmp
);
78 static inline int __cw1200_reg_write_32(struct cw1200_common
*priv
,
81 __le32 tmp
= cpu_to_le32(val
);
82 return __cw1200_reg_write(priv
, addr
, &tmp
, sizeof(tmp
), 0);
85 static inline int __cw1200_reg_read_16(struct cw1200_common
*priv
,
89 int i
= __cw1200_reg_read(priv
, addr
, &tmp
, sizeof(tmp
), 0);
90 *val
= le16_to_cpu(tmp
);
94 static inline int __cw1200_reg_write_16(struct cw1200_common
*priv
,
97 __le16 tmp
= cpu_to_le16(val
);
98 return __cw1200_reg_write(priv
, addr
, &tmp
, sizeof(tmp
), 0);
101 int cw1200_reg_read(struct cw1200_common
*priv
, u16 addr
, void *buf
,
105 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
106 ret
= __cw1200_reg_read(priv
, addr
, buf
, buf_len
, 0);
107 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
111 int cw1200_reg_write(struct cw1200_common
*priv
, u16 addr
, const void *buf
,
115 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
116 ret
= __cw1200_reg_write(priv
, addr
, buf
, buf_len
, 0);
117 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
121 int cw1200_data_read(struct cw1200_common
*priv
, void *buf
, size_t buf_len
)
124 int buf_id_rx
= priv
->buf_id_rx
;
126 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
128 while (retry
<= MAX_RETRY
) {
129 ret
= __cw1200_reg_read(priv
,
130 ST90TDS_IN_OUT_QUEUE_REG_ID
, buf
,
131 buf_len
, buf_id_rx
+ 1);
133 buf_id_rx
= (buf_id_rx
+ 1) & 3;
134 priv
->buf_id_rx
= buf_id_rx
;
139 pr_err("error :[%d]\n", ret
);
143 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
147 int cw1200_data_write(struct cw1200_common
*priv
, const void *buf
,
151 int buf_id_tx
= priv
->buf_id_tx
;
153 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
155 while (retry
<= MAX_RETRY
) {
156 ret
= __cw1200_reg_write(priv
,
157 ST90TDS_IN_OUT_QUEUE_REG_ID
, buf
,
160 buf_id_tx
= (buf_id_tx
+ 1) & 31;
161 priv
->buf_id_tx
= buf_id_tx
;
166 pr_err("error :[%d]\n", ret
);
170 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
174 int cw1200_indirect_read(struct cw1200_common
*priv
, u32 addr
, void *buf
,
175 size_t buf_len
, u32 prefetch
, u16 port_addr
)
180 if ((buf_len
/ 2) >= 0x1000) {
181 pr_err("Can't read more than 0xfff words.\n");
185 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
187 ret
= __cw1200_reg_write_32(priv
, ST90TDS_SRAM_BASE_ADDR_REG_ID
, addr
);
189 pr_err("Can't write address register.\n");
193 /* Read CONFIG Register Value - We will read 32 bits */
194 ret
= __cw1200_reg_read_32(priv
, ST90TDS_CONFIG_REG_ID
, &val32
);
196 pr_err("Can't read config register.\n");
200 /* Set PREFETCH bit */
201 ret
= __cw1200_reg_write_32(priv
, ST90TDS_CONFIG_REG_ID
,
204 pr_err("Can't write prefetch bit.\n");
208 /* Check for PRE-FETCH bit to be cleared */
209 for (i
= 0; i
< 20; i
++) {
210 ret
= __cw1200_reg_read_32(priv
, ST90TDS_CONFIG_REG_ID
, &val32
);
212 pr_err("Can't check prefetch bit.\n");
215 if (!(val32
& prefetch
))
221 if (val32
& prefetch
) {
222 pr_err("Prefetch bit is not cleared.\n");
227 ret
= __cw1200_reg_read(priv
, port_addr
, buf
, buf_len
, 0);
229 pr_err("Can't read data port.\n");
234 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
238 int cw1200_apb_write(struct cw1200_common
*priv
, u32 addr
, const void *buf
,
243 if ((buf_len
/ 2) >= 0x1000) {
244 pr_err("Can't write more than 0xfff words.\n");
248 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
251 ret
= __cw1200_reg_write_32(priv
, ST90TDS_SRAM_BASE_ADDR_REG_ID
, addr
);
253 pr_err("Can't write address register.\n");
257 /* Write data port */
258 ret
= __cw1200_reg_write(priv
, ST90TDS_SRAM_DPORT_REG_ID
,
261 pr_err("Can't write data port.\n");
266 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
270 int __cw1200_irq_enable(struct cw1200_common
*priv
, int enable
)
276 /* We need to do this hack because the SPI layer can sleep on I/O
277 and the general path involves I/O to the device in interrupt
280 However, the initial enable call needs to go to the hardware.
282 We don't worry about shutdown because we do a full reset which
283 clears the interrupt enabled bits.
285 if (priv
->hwbus_ops
->irq_enable
) {
286 ret
= priv
->hwbus_ops
->irq_enable(priv
->hwbus_priv
, enable
);
287 if (ret
|| enable
< 2)
291 if (HIF_8601_SILICON
== priv
->hw_type
) {
292 ret
= __cw1200_reg_read_32(priv
, ST90TDS_CONFIG_REG_ID
, &val32
);
294 pr_err("Can't read config register.\n");
299 val32
|= ST90TDS_CONF_IRQ_RDY_ENABLE
;
301 val32
&= ~ST90TDS_CONF_IRQ_RDY_ENABLE
;
303 ret
= __cw1200_reg_write_32(priv
, ST90TDS_CONFIG_REG_ID
, val32
);
305 pr_err("Can't write config register.\n");
309 ret
= __cw1200_reg_read_16(priv
, ST90TDS_CONFIG_REG_ID
, &val16
);
311 pr_err("Can't read control register.\n");
316 val16
|= ST90TDS_CONT_IRQ_RDY_ENABLE
;
318 val16
&= ~ST90TDS_CONT_IRQ_RDY_ENABLE
;
320 ret
= __cw1200_reg_write_16(priv
, ST90TDS_CONFIG_REG_ID
, val16
);
322 pr_err("Can't write control register.\n");