omap_uwire.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. /* uWire Registers: */
  58. #define UWIRE_IO_SIZE 0x20
  59. #define UWIRE_TDR 0x00
  60. #define UWIRE_RDR 0x00
  61. #define UWIRE_CSR 0x01
  62. #define UWIRE_SR1 0x02
  63. #define UWIRE_SR2 0x03
  64. #define UWIRE_SR3 0x04
  65. #define UWIRE_SR4 0x05
  66. #define UWIRE_SR5 0x06
  67. /* CSR bits */
  68. #define RDRB (1 << 15)
  69. #define CSRB (1 << 14)
  70. #define START (1 << 13)
  71. #define CS_CMD (1 << 12)
  72. /* SR1 or SR2 bits */
  73. #define UWIRE_READ_FALLING_EDGE 0x0001
  74. #define UWIRE_READ_RISING_EDGE 0x0000
  75. #define UWIRE_WRITE_FALLING_EDGE 0x0000
  76. #define UWIRE_WRITE_RISING_EDGE 0x0002
  77. #define UWIRE_CS_ACTIVE_LOW 0x0000
  78. #define UWIRE_CS_ACTIVE_HIGH 0x0004
  79. #define UWIRE_FREQ_DIV_2 0x0000
  80. #define UWIRE_FREQ_DIV_4 0x0008
  81. #define UWIRE_FREQ_DIV_8 0x0010
  82. #define UWIRE_CHK_READY 0x0020
  83. #define UWIRE_CLK_INVERTED 0x0040
  84. struct uwire_spi {
  85. struct spi_bitbang bitbang;
  86. struct clk *ck;
  87. };
  88. struct uwire_state {
  89. unsigned bits_per_word;
  90. unsigned div1_idx;
  91. };
  92. /* REVISIT compile time constant for idx_shift? */
  93. /*
  94. * Or, put it in a structure which is used throughout the driver;
  95. * that avoids having to issue two loads for each bit of static data.
  96. */
  97. static unsigned int uwire_idx_shift;
  98. static void __iomem *uwire_base;
  99. static inline void uwire_write_reg(int idx, u16 val)
  100. {
  101. __raw_writew(val, uwire_base + (idx << uwire_idx_shift));
  102. }
  103. static inline u16 uwire_read_reg(int idx)
  104. {
  105. return __raw_readw(uwire_base + (idx << uwire_idx_shift));
  106. }
  107. static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
  108. {
  109. u16 w, val = 0;
  110. int shift, reg;
  111. if (flags & UWIRE_CLK_INVERTED)
  112. val ^= 0x03;
  113. val = flags & 0x3f;
  114. if (cs & 1)
  115. shift = 6;
  116. else
  117. shift = 0;
  118. if (cs <= 1)
  119. reg = UWIRE_SR1;
  120. else
  121. reg = UWIRE_SR2;
  122. w = uwire_read_reg(reg);
  123. w &= ~(0x3f << shift);
  124. w |= val << shift;
  125. uwire_write_reg(reg, w);
  126. }
  127. static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
  128. {
  129. u16 w;
  130. int c = 0;
  131. unsigned long max_jiffies = jiffies + HZ;
  132. for (;;) {
  133. w = uwire_read_reg(UWIRE_CSR);
  134. if ((w & mask) == val)
  135. break;
  136. if (time_after(jiffies, max_jiffies)) {
  137. printk(KERN_ERR "%s: timeout. reg=%#06x "
  138. "mask=%#06x val=%#06x\n",
  139. __func__, w, mask, val);
  140. return -1;
  141. }
  142. c++;
  143. if (might_not_catch && c > 64)
  144. break;
  145. }
  146. return 0;
  147. }
  148. static void uwire_set_clk1_div(int div1_idx)
  149. {
  150. u16 w;
  151. w = uwire_read_reg(UWIRE_SR3);
  152. w &= ~(0x03 << 1);
  153. w |= div1_idx << 1;
  154. uwire_write_reg(UWIRE_SR3, w);
  155. }
  156. static void uwire_chipselect(struct spi_device *spi, int value)
  157. {
  158. struct uwire_state *ust = spi->controller_state;
  159. u16 w;
  160. int old_cs;
  161. BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
  162. w = uwire_read_reg(UWIRE_CSR);
  163. old_cs = (w >> 10) & 0x03;
  164. if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) {
  165. /* Deselect this CS, or the previous CS */
  166. w &= ~CS_CMD;
  167. uwire_write_reg(UWIRE_CSR, w);
  168. }
  169. /* activate specfied chipselect */
  170. if (value == BITBANG_CS_ACTIVE) {
  171. uwire_set_clk1_div(ust->div1_idx);
  172. /* invert clock? */
  173. if (spi->mode & SPI_CPOL)
  174. uwire_write_reg(UWIRE_SR4, 1);
  175. else
  176. uwire_write_reg(UWIRE_SR4, 0);
  177. w = spi->chip_select << 10;
  178. w |= CS_CMD;
  179. uwire_write_reg(UWIRE_CSR, w);
  180. }
  181. }
  182. static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
  183. {
  184. struct uwire_state *ust = spi->controller_state;
  185. unsigned len = t->len;
  186. unsigned bits = ust->bits_per_word;
  187. unsigned bytes;
  188. u16 val, w;
  189. int status = 0;;
  190. if (!t->tx_buf && !t->rx_buf)
  191. return 0;
  192. /* Microwire doesn't read and write concurrently */
  193. if (t->tx_buf && t->rx_buf)
  194. return -EPERM;
  195. w = spi->chip_select << 10;
  196. w |= CS_CMD;
  197. if (t->tx_buf) {
  198. const u8 *buf = t->tx_buf;
  199. /* NOTE: DMA could be used for TX transfers */
  200. /* write one or two bytes at a time */
  201. while (len >= 1) {
  202. /* tx bit 15 is first sent; we byteswap multibyte words
  203. * (msb-first) on the way out from memory.
  204. */
  205. val = *buf++;
  206. if (bits > 8) {
  207. bytes = 2;
  208. val |= *buf++ << 8;
  209. } else
  210. bytes = 1;
  211. val <<= 16 - bits;
  212. #ifdef VERBOSE
  213. pr_debug("%s: write-%d =%04x\n",
  214. dev_name(&spi->dev), bits, val);
  215. #endif
  216. if (wait_uwire_csr_flag(CSRB, 0, 0))
  217. goto eio;
  218. uwire_write_reg(UWIRE_TDR, val);
  219. /* start write */
  220. val = START | w | (bits << 5);
  221. uwire_write_reg(UWIRE_CSR, val);
  222. len -= bytes;
  223. /* Wait till write actually starts.
  224. * This is needed with MPU clock 60+ MHz.
  225. * REVISIT: we may not have time to catch it...
  226. */
  227. if (wait_uwire_csr_flag(CSRB, CSRB, 1))
  228. goto eio;
  229. status += bytes;
  230. }
  231. /* REVISIT: save this for later to get more i/o overlap */
  232. if (wait_uwire_csr_flag(CSRB, 0, 0))
  233. goto eio;
  234. } else if (t->rx_buf) {
  235. u8 *buf = t->rx_buf;
  236. /* read one or two bytes at a time */
  237. while (len) {
  238. if (bits > 8) {
  239. bytes = 2;
  240. } else
  241. bytes = 1;
  242. /* start read */
  243. val = START | w | (bits << 0);
  244. uwire_write_reg(UWIRE_CSR, val);
  245. len -= bytes;
  246. /* Wait till read actually starts */
  247. (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
  248. if (wait_uwire_csr_flag(RDRB | CSRB,
  249. RDRB, 0))
  250. goto eio;
  251. /* rx bit 0 is last received; multibyte words will
  252. * be properly byteswapped on the way to memory.
  253. */
  254. val = uwire_read_reg(UWIRE_RDR);
  255. val &= (1 << bits) - 1;
  256. *buf++ = (u8) val;
  257. if (bytes == 2)
  258. *buf++ = val >> 8;
  259. status += bytes;
  260. #ifdef VERBOSE
  261. pr_debug("%s: read-%d =%04x\n",
  262. dev_name(&spi->dev), bits, val);
  263. #endif
  264. }
  265. }
  266. return status;
  267. eio:
  268. return -EIO;
  269. }
  270. static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  271. {
  272. struct uwire_state *ust = spi->controller_state;
  273. struct uwire_spi *uwire;
  274. unsigned flags = 0;
  275. unsigned bits;
  276. unsigned hz;
  277. unsigned long rate;
  278. int div1_idx;
  279. int div1;
  280. int div2;
  281. int status;
  282. uwire = spi_master_get_devdata(spi->master);
  283. if (spi->chip_select > 3) {
  284. pr_debug("%s: cs%d?\n", dev_name(&spi->dev), spi->chip_select);
  285. status = -ENODEV;
  286. goto done;
  287. }
  288. bits = spi->bits_per_word;
  289. if (t != NULL && t->bits_per_word)
  290. bits = t->bits_per_word;
  291. if (!bits)
  292. bits = 8;
  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. /* the spi->mode bits understood by this driver: */
  390. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  391. static int uwire_setup(struct spi_device *spi)
  392. {
  393. struct uwire_state *ust = spi->controller_state;
  394. if (spi->mode & ~MODEBITS) {
  395. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  396. spi->mode & ~MODEBITS);
  397. return -EINVAL;
  398. }
  399. if (ust == NULL) {
  400. ust = kzalloc(sizeof(*ust), GFP_KERNEL);
  401. if (ust == NULL)
  402. return -ENOMEM;
  403. spi->controller_state = ust;
  404. }
  405. return uwire_setup_transfer(spi, NULL);
  406. }
  407. static void uwire_cleanup(struct spi_device *spi)
  408. {
  409. kfree(spi->controller_state);
  410. }
  411. static void uwire_off(struct uwire_spi *uwire)
  412. {
  413. uwire_write_reg(UWIRE_SR3, 0);
  414. clk_disable(uwire->ck);
  415. clk_put(uwire->ck);
  416. spi_master_put(uwire->bitbang.master);
  417. }
  418. static int __init uwire_probe(struct platform_device *pdev)
  419. {
  420. struct spi_master *master;
  421. struct uwire_spi *uwire;
  422. int status;
  423. master = spi_alloc_master(&pdev->dev, sizeof *uwire);
  424. if (!master)
  425. return -ENODEV;
  426. uwire = spi_master_get_devdata(master);
  427. uwire_base = ioremap(UWIRE_BASE_PHYS, UWIRE_IO_SIZE);
  428. if (!uwire_base) {
  429. dev_dbg(&pdev->dev, "can't ioremap UWIRE\n");
  430. spi_master_put(master);
  431. return -ENOMEM;
  432. }
  433. dev_set_drvdata(&pdev->dev, uwire);
  434. uwire->ck = clk_get(&pdev->dev, "armxor_ck");
  435. if (!uwire->ck || IS_ERR(uwire->ck)) {
  436. dev_dbg(&pdev->dev, "no mpu_xor_clk ?\n");
  437. spi_master_put(master);
  438. return -ENODEV;
  439. }
  440. clk_enable(uwire->ck);
  441. if (cpu_is_omap730())
  442. uwire_idx_shift = 1;
  443. else
  444. uwire_idx_shift = 2;
  445. uwire_write_reg(UWIRE_SR3, 1);
  446. master->bus_num = 2; /* "official" */
  447. master->num_chipselect = 4;
  448. master->setup = uwire_setup;
  449. master->cleanup = uwire_cleanup;
  450. uwire->bitbang.master = master;
  451. uwire->bitbang.chipselect = uwire_chipselect;
  452. uwire->bitbang.setup_transfer = uwire_setup_transfer;
  453. uwire->bitbang.txrx_bufs = uwire_txrx;
  454. status = spi_bitbang_start(&uwire->bitbang);
  455. if (status < 0) {
  456. uwire_off(uwire);
  457. iounmap(uwire_base);
  458. }
  459. return status;
  460. }
  461. static int __exit uwire_remove(struct platform_device *pdev)
  462. {
  463. struct uwire_spi *uwire = dev_get_drvdata(&pdev->dev);
  464. int status;
  465. // FIXME remove all child devices, somewhere ...
  466. status = spi_bitbang_stop(&uwire->bitbang);
  467. uwire_off(uwire);
  468. iounmap(uwire_base);
  469. return status;
  470. }
  471. /* work with hotplug and coldplug */
  472. MODULE_ALIAS("platform:omap_uwire");
  473. static struct platform_driver uwire_driver = {
  474. .driver = {
  475. .name = "omap_uwire",
  476. .owner = THIS_MODULE,
  477. },
  478. .remove = __exit_p(uwire_remove),
  479. // suspend ... unuse ck
  480. // resume ... use ck
  481. };
  482. static int __init omap_uwire_init(void)
  483. {
  484. /* FIXME move these into the relevant board init code. also, include
  485. * H3 support; it uses tsc2101 like H2 (on a different chipselect).
  486. */
  487. if (machine_is_omap_h2()) {
  488. /* defaults: W21 SDO, U18 SDI, V19 SCL */
  489. omap_cfg_reg(N14_1610_UWIRE_CS0);
  490. omap_cfg_reg(N15_1610_UWIRE_CS1);
  491. }
  492. if (machine_is_omap_perseus2()) {
  493. /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
  494. int val = omap_readl(OMAP730_IO_CONF_9) & ~0x00EEE000;
  495. omap_writel(val | 0x00AAA000, OMAP730_IO_CONF_9);
  496. }
  497. return platform_driver_probe(&uwire_driver, uwire_probe);
  498. }
  499. static void __exit omap_uwire_exit(void)
  500. {
  501. platform_driver_unregister(&uwire_driver);
  502. }
  503. subsys_initcall(omap_uwire_init);
  504. module_exit(omap_uwire_exit);
  505. MODULE_LICENSE("GPL");