spi-omap-uwire.c 12 KB

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