omap_uwire.c 13 KB

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