spi-omap-uwire.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * MicroWire interface driver for OMAP
  3. *
  4. * Copyright 2003 MontaVista Software Inc. <source@mvista.com>
  5. *
  6. * Ported to 2.6 OMAP uwire interface.
  7. * Copyright (C) 2004 Texas Instruments.
  8. *
  9. * Generalization patches by Juha Yrjola <juha.yrjola@nokia.com>
  10. *
  11. * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface)
  12. * Copyright (C) 2006 Nokia
  13. *
  14. * Many updates by Imre Deak <imre.deak@nokia.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the
  18. * Free Software Foundation; either version 2 of the License, or (at your
  19. * option) any later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  27. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * You should have received a copy of the GNU General Public License along
  33. * with this program; if not, write to the Free Software Foundation, Inc.,
  34. * 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/delay.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/err.h>
  43. #include <linux/clk.h>
  44. #include <linux/slab.h>
  45. #include <linux/spi/spi.h>
  46. #include <linux/spi/spi_bitbang.h>
  47. #include <linux/module.h>
  48. #include <asm/system.h>
  49. #include <asm/irq.h>
  50. #include <mach/hardware.h>
  51. #include <asm/io.h>
  52. #include <asm/mach-types.h>
  53. #include <plat/mux.h>
  54. #include <plat/omap7xx.h> /* OMAP7XX_IO_CONF registers */
  55. /* FIXME address is now a platform device resource,
  56. * and irqs should show there too...
  57. */
  58. #define UWIRE_BASE_PHYS 0xFFFB3000
  59. /* uWire Registers: */
  60. #define UWIRE_IO_SIZE 0x20
  61. #define UWIRE_TDR 0x00
  62. #define UWIRE_RDR 0x00
  63. #define UWIRE_CSR 0x01
  64. #define UWIRE_SR1 0x02
  65. #define UWIRE_SR2 0x03
  66. #define UWIRE_SR3 0x04
  67. #define UWIRE_SR4 0x05
  68. #define UWIRE_SR5 0x06
  69. /* CSR bits */
  70. #define RDRB (1 << 15)
  71. #define CSRB (1 << 14)
  72. #define START (1 << 13)
  73. #define CS_CMD (1 << 12)
  74. /* SR1 or SR2 bits */
  75. #define UWIRE_READ_FALLING_EDGE 0x0001
  76. #define UWIRE_READ_RISING_EDGE 0x0000
  77. #define UWIRE_WRITE_FALLING_EDGE 0x0000
  78. #define UWIRE_WRITE_RISING_EDGE 0x0002
  79. #define UWIRE_CS_ACTIVE_LOW 0x0000
  80. #define UWIRE_CS_ACTIVE_HIGH 0x0004
  81. #define UWIRE_FREQ_DIV_2 0x0000
  82. #define UWIRE_FREQ_DIV_4 0x0008
  83. #define UWIRE_FREQ_DIV_8 0x0010
  84. #define UWIRE_CHK_READY 0x0020
  85. #define UWIRE_CLK_INVERTED 0x0040
  86. struct uwire_spi {
  87. struct spi_bitbang bitbang;
  88. struct clk *ck;
  89. };
  90. struct uwire_state {
  91. unsigned bits_per_word;
  92. unsigned div1_idx;
  93. };
  94. /* REVISIT compile time constant for idx_shift? */
  95. /*
  96. * Or, put it in a structure which is used throughout the driver;
  97. * that avoids having to issue two loads for each bit of static data.
  98. */
  99. static unsigned int uwire_idx_shift;
  100. static void __iomem *uwire_base;
  101. static inline void uwire_write_reg(int idx, u16 val)
  102. {
  103. __raw_writew(val, uwire_base + (idx << uwire_idx_shift));
  104. }
  105. static inline u16 uwire_read_reg(int idx)
  106. {
  107. return __raw_readw(uwire_base + (idx << uwire_idx_shift));
  108. }
  109. static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
  110. {
  111. u16 w, val = 0;
  112. int shift, reg;
  113. if (flags & UWIRE_CLK_INVERTED)
  114. val ^= 0x03;
  115. val = flags & 0x3f;
  116. if (cs & 1)
  117. shift = 6;
  118. else
  119. shift = 0;
  120. if (cs <= 1)
  121. reg = UWIRE_SR1;
  122. else
  123. reg = UWIRE_SR2;
  124. w = uwire_read_reg(reg);
  125. w &= ~(0x3f << shift);
  126. w |= val << shift;
  127. uwire_write_reg(reg, w);
  128. }
  129. static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
  130. {
  131. u16 w;
  132. int c = 0;
  133. unsigned long max_jiffies = jiffies + HZ;
  134. for (;;) {
  135. w = uwire_read_reg(UWIRE_CSR);
  136. if ((w & mask) == val)
  137. break;
  138. if (time_after(jiffies, max_jiffies)) {
  139. printk(KERN_ERR "%s: timeout. reg=%#06x "
  140. "mask=%#06x val=%#06x\n",
  141. __func__, w, mask, val);
  142. return -1;
  143. }
  144. c++;
  145. if (might_not_catch && c > 64)
  146. break;
  147. }
  148. return 0;
  149. }
  150. static void uwire_set_clk1_div(int div1_idx)
  151. {
  152. u16 w;
  153. w = uwire_read_reg(UWIRE_SR3);
  154. w &= ~(0x03 << 1);
  155. w |= div1_idx << 1;
  156. uwire_write_reg(UWIRE_SR3, w);
  157. }
  158. static void uwire_chipselect(struct spi_device *spi, int value)
  159. {
  160. struct uwire_state *ust = spi->controller_state;
  161. u16 w;
  162. int old_cs;
  163. BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
  164. w = uwire_read_reg(UWIRE_CSR);
  165. old_cs = (w >> 10) & 0x03;
  166. if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) {
  167. /* Deselect this CS, or the previous CS */
  168. w &= ~CS_CMD;
  169. uwire_write_reg(UWIRE_CSR, w);
  170. }
  171. /* activate specfied chipselect */
  172. if (value == BITBANG_CS_ACTIVE) {
  173. uwire_set_clk1_div(ust->div1_idx);
  174. /* invert clock? */
  175. if (spi->mode & SPI_CPOL)
  176. uwire_write_reg(UWIRE_SR4, 1);
  177. else
  178. uwire_write_reg(UWIRE_SR4, 0);
  179. w = spi->chip_select << 10;
  180. w |= CS_CMD;
  181. uwire_write_reg(UWIRE_CSR, w);
  182. }
  183. }
  184. static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
  185. {
  186. struct uwire_state *ust = spi->controller_state;
  187. unsigned len = t->len;
  188. unsigned bits = ust->bits_per_word;
  189. unsigned bytes;
  190. u16 val, w;
  191. int status = 0;
  192. if (!t->tx_buf && !t->rx_buf)
  193. return 0;
  194. /* Microwire doesn't read and write concurrently */
  195. if (t->tx_buf && t->rx_buf)
  196. return -EPERM;
  197. w = spi->chip_select << 10;
  198. w |= CS_CMD;
  199. if (t->tx_buf) {
  200. const u8 *buf = t->tx_buf;
  201. /* NOTE: DMA could be used for TX transfers */
  202. /* write one or two bytes at a time */
  203. while (len >= 1) {
  204. /* tx bit 15 is first sent; we byteswap multibyte words
  205. * (msb-first) on the way out from memory.
  206. */
  207. val = *buf++;
  208. if (bits > 8) {
  209. bytes = 2;
  210. val |= *buf++ << 8;
  211. } else
  212. bytes = 1;
  213. val <<= 16 - bits;
  214. #ifdef VERBOSE
  215. pr_debug("%s: write-%d =%04x\n",
  216. dev_name(&spi->dev), bits, val);
  217. #endif
  218. if (wait_uwire_csr_flag(CSRB, 0, 0))
  219. goto eio;
  220. uwire_write_reg(UWIRE_TDR, val);
  221. /* start write */
  222. val = START | w | (bits << 5);
  223. uwire_write_reg(UWIRE_CSR, val);
  224. len -= bytes;
  225. /* Wait till write actually starts.
  226. * This is needed with MPU clock 60+ MHz.
  227. * REVISIT: we may not have time to catch it...
  228. */
  229. if (wait_uwire_csr_flag(CSRB, CSRB, 1))
  230. goto eio;
  231. status += bytes;
  232. }
  233. /* REVISIT: save this for later to get more i/o overlap */
  234. if (wait_uwire_csr_flag(CSRB, 0, 0))
  235. goto eio;
  236. } else if (t->rx_buf) {
  237. u8 *buf = t->rx_buf;
  238. /* read one or two bytes at a time */
  239. while (len) {
  240. if (bits > 8) {
  241. bytes = 2;
  242. } else
  243. bytes = 1;
  244. /* start read */
  245. val = START | w | (bits << 0);
  246. uwire_write_reg(UWIRE_CSR, val);
  247. len -= bytes;
  248. /* Wait till read actually starts */
  249. (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
  250. if (wait_uwire_csr_flag(RDRB | CSRB,
  251. RDRB, 0))
  252. goto eio;
  253. /* rx bit 0 is last received; multibyte words will
  254. * be properly byteswapped on the way to memory.
  255. */
  256. val = uwire_read_reg(UWIRE_RDR);
  257. val &= (1 << bits) - 1;
  258. *buf++ = (u8) val;
  259. if (bytes == 2)
  260. *buf++ = val >> 8;
  261. status += bytes;
  262. #ifdef VERBOSE
  263. pr_debug("%s: read-%d =%04x\n",
  264. dev_name(&spi->dev), bits, val);
  265. #endif
  266. }
  267. }
  268. return status;
  269. eio:
  270. return -EIO;
  271. }
  272. static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  273. {
  274. struct uwire_state *ust = spi->controller_state;
  275. struct uwire_spi *uwire;
  276. unsigned flags = 0;
  277. unsigned bits;
  278. unsigned hz;
  279. unsigned long rate;
  280. int div1_idx;
  281. int div1;
  282. int div2;
  283. int status;
  284. uwire = spi_master_get_devdata(spi->master);
  285. if (spi->chip_select > 3) {
  286. pr_debug("%s: cs%d?\n", dev_name(&spi->dev), spi->chip_select);
  287. status = -ENODEV;
  288. goto done;
  289. }
  290. bits = spi->bits_per_word;
  291. if (t != NULL && t->bits_per_word)
  292. bits = t->bits_per_word;
  293. if (bits > 16) {
  294. pr_debug("%s: wordsize %d?\n", dev_name(&spi->dev), bits);
  295. status = -ENODEV;
  296. goto done;
  297. }
  298. ust->bits_per_word = bits;
  299. /* mode 0..3, clock inverted separately;
  300. * standard nCS signaling;
  301. * don't treat DI=high as "not ready"
  302. */
  303. if (spi->mode & SPI_CS_HIGH)
  304. flags |= UWIRE_CS_ACTIVE_HIGH;
  305. if (spi->mode & SPI_CPOL)
  306. flags |= UWIRE_CLK_INVERTED;
  307. switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
  308. case SPI_MODE_0:
  309. case SPI_MODE_3:
  310. flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
  311. break;
  312. case SPI_MODE_1:
  313. case SPI_MODE_2:
  314. flags |= UWIRE_WRITE_RISING_EDGE | UWIRE_READ_FALLING_EDGE;
  315. break;
  316. }
  317. /* assume it's already enabled */
  318. rate = clk_get_rate(uwire->ck);
  319. hz = spi->max_speed_hz;
  320. if (t != NULL && t->speed_hz)
  321. hz = t->speed_hz;
  322. if (!hz) {
  323. pr_debug("%s: zero speed?\n", dev_name(&spi->dev));
  324. status = -EINVAL;
  325. goto done;
  326. }
  327. /* F_INT = mpu_xor_clk / DIV1 */
  328. for (div1_idx = 0; div1_idx < 4; div1_idx++) {
  329. switch (div1_idx) {
  330. case 0:
  331. div1 = 2;
  332. break;
  333. case 1:
  334. div1 = 4;
  335. break;
  336. case 2:
  337. div1 = 7;
  338. break;
  339. default:
  340. case 3:
  341. div1 = 10;
  342. break;
  343. }
  344. div2 = (rate / div1 + hz - 1) / hz;
  345. if (div2 <= 8)
  346. break;
  347. }
  348. if (div1_idx == 4) {
  349. pr_debug("%s: lowest clock %ld, need %d\n",
  350. dev_name(&spi->dev), rate / 10 / 8, hz);
  351. status = -EDOM;
  352. goto done;
  353. }
  354. /* we have to cache this and reset in uwire_chipselect as this is a
  355. * global parameter and another uwire device can change it under
  356. * us */
  357. ust->div1_idx = div1_idx;
  358. uwire_set_clk1_div(div1_idx);
  359. rate /= div1;
  360. switch (div2) {
  361. case 0:
  362. case 1:
  363. case 2:
  364. flags |= UWIRE_FREQ_DIV_2;
  365. rate /= 2;
  366. break;
  367. case 3:
  368. case 4:
  369. flags |= UWIRE_FREQ_DIV_4;
  370. rate /= 4;
  371. break;
  372. case 5:
  373. case 6:
  374. case 7:
  375. case 8:
  376. flags |= UWIRE_FREQ_DIV_8;
  377. rate /= 8;
  378. break;
  379. }
  380. omap_uwire_configure_mode(spi->chip_select, flags);
  381. pr_debug("%s: uwire flags %02x, armxor %lu KHz, SCK %lu KHz\n",
  382. __func__, flags,
  383. clk_get_rate(uwire->ck) / 1000,
  384. rate / 1000);
  385. status = 0;
  386. done:
  387. return status;
  388. }
  389. static int uwire_setup(struct spi_device *spi)
  390. {
  391. struct uwire_state *ust = spi->controller_state;
  392. if (ust == NULL) {
  393. ust = kzalloc(sizeof(*ust), GFP_KERNEL);
  394. if (ust == NULL)
  395. return -ENOMEM;
  396. spi->controller_state = ust;
  397. }
  398. return uwire_setup_transfer(spi, NULL);
  399. }
  400. static void uwire_cleanup(struct spi_device *spi)
  401. {
  402. kfree(spi->controller_state);
  403. }
  404. static void uwire_off(struct uwire_spi *uwire)
  405. {
  406. uwire_write_reg(UWIRE_SR3, 0);
  407. clk_disable(uwire->ck);
  408. clk_put(uwire->ck);
  409. spi_master_put(uwire->bitbang.master);
  410. }
  411. static int __init uwire_probe(struct platform_device *pdev)
  412. {
  413. struct spi_master *master;
  414. struct uwire_spi *uwire;
  415. int status;
  416. master = spi_alloc_master(&pdev->dev, sizeof *uwire);
  417. if (!master)
  418. return -ENODEV;
  419. uwire = spi_master_get_devdata(master);
  420. uwire_base = ioremap(UWIRE_BASE_PHYS, UWIRE_IO_SIZE);
  421. if (!uwire_base) {
  422. dev_dbg(&pdev->dev, "can't ioremap UWIRE\n");
  423. spi_master_put(master);
  424. return -ENOMEM;
  425. }
  426. dev_set_drvdata(&pdev->dev, uwire);
  427. uwire->ck = clk_get(&pdev->dev, "fck");
  428. if (IS_ERR(uwire->ck)) {
  429. status = PTR_ERR(uwire->ck);
  430. dev_dbg(&pdev->dev, "no functional clock?\n");
  431. spi_master_put(master);
  432. return status;
  433. }
  434. clk_enable(uwire->ck);
  435. if (cpu_is_omap7xx())
  436. uwire_idx_shift = 1;
  437. else
  438. uwire_idx_shift = 2;
  439. uwire_write_reg(UWIRE_SR3, 1);
  440. /* the spi->mode bits understood by this driver: */
  441. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  442. master->flags = SPI_MASTER_HALF_DUPLEX;
  443. master->bus_num = 2; /* "official" */
  444. master->num_chipselect = 4;
  445. master->setup = uwire_setup;
  446. master->cleanup = uwire_cleanup;
  447. uwire->bitbang.master = master;
  448. uwire->bitbang.chipselect = uwire_chipselect;
  449. uwire->bitbang.setup_transfer = uwire_setup_transfer;
  450. uwire->bitbang.txrx_bufs = uwire_txrx;
  451. status = spi_bitbang_start(&uwire->bitbang);
  452. if (status < 0) {
  453. uwire_off(uwire);
  454. iounmap(uwire_base);
  455. }
  456. return status;
  457. }
  458. static int __exit uwire_remove(struct platform_device *pdev)
  459. {
  460. struct uwire_spi *uwire = dev_get_drvdata(&pdev->dev);
  461. int status;
  462. // FIXME remove all child devices, somewhere ...
  463. status = spi_bitbang_stop(&uwire->bitbang);
  464. uwire_off(uwire);
  465. iounmap(uwire_base);
  466. return status;
  467. }
  468. /* work with hotplug and coldplug */
  469. MODULE_ALIAS("platform:omap_uwire");
  470. static struct platform_driver uwire_driver = {
  471. .driver = {
  472. .name = "omap_uwire",
  473. .owner = THIS_MODULE,
  474. },
  475. .remove = __exit_p(uwire_remove),
  476. // suspend ... unuse ck
  477. // resume ... use ck
  478. };
  479. static int __init omap_uwire_init(void)
  480. {
  481. /* FIXME move these into the relevant board init code. also, include
  482. * H3 support; it uses tsc2101 like H2 (on a different chipselect).
  483. */
  484. if (machine_is_omap_h2()) {
  485. /* defaults: W21 SDO, U18 SDI, V19 SCL */
  486. omap_cfg_reg(N14_1610_UWIRE_CS0);
  487. omap_cfg_reg(N15_1610_UWIRE_CS1);
  488. }
  489. if (machine_is_omap_perseus2()) {
  490. /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
  491. int val = omap_readl(OMAP7XX_IO_CONF_9) & ~0x00EEE000;
  492. omap_writel(val | 0x00AAA000, OMAP7XX_IO_CONF_9);
  493. }
  494. return platform_driver_probe(&uwire_driver, uwire_probe);
  495. }
  496. static void __exit omap_uwire_exit(void)
  497. {
  498. platform_driver_unregister(&uwire_driver);
  499. }
  500. subsys_initcall(omap_uwire_init);
  501. module_exit(omap_uwire_exit);
  502. MODULE_LICENSE("GPL");