spi-omap-100k.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * OMAP7xx SPI 100k controller driver
  3. * Author: Fabrice Crohas <fcrohas@gmail.com>
  4. * from original omap1_mcspi driver
  5. *
  6. * Copyright (C) 2005, 2006 Nokia Corporation
  7. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  8. * Juha Yrj�l� <juha.yrjola@nokia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/err.h>
  33. #include <linux/clk.h>
  34. #include <linux/io.h>
  35. #include <linux/gpio.h>
  36. #include <linux/slab.h>
  37. #include <linux/spi/spi.h>
  38. #define OMAP1_SPI100K_MAX_FREQ 48000000
  39. #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12)
  40. #define SPI_SETUP1 0x00
  41. #define SPI_SETUP2 0x02
  42. #define SPI_CTRL 0x04
  43. #define SPI_STATUS 0x06
  44. #define SPI_TX_LSB 0x08
  45. #define SPI_TX_MSB 0x0a
  46. #define SPI_RX_LSB 0x0c
  47. #define SPI_RX_MSB 0x0e
  48. #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5)
  49. #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4)
  50. #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1)
  51. #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0)
  52. #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0)
  53. #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0)
  54. #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5)
  55. #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5)
  56. #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10)
  57. #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10)
  58. #define SPI_CTRL_SEN(x) ((x) << 7)
  59. #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2)
  60. #define SPI_CTRL_WR (1UL << 1)
  61. #define SPI_CTRL_RD (1UL << 0)
  62. #define SPI_STATUS_WE (1UL << 1)
  63. #define SPI_STATUS_RD (1UL << 0)
  64. #define WRITE 0
  65. #define READ 1
  66. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  67. * cache operations; better heuristics consider wordsize and bitrate.
  68. */
  69. #define DMA_MIN_BYTES 8
  70. #define SPI_RUNNING 0
  71. #define SPI_SHUTDOWN 1
  72. struct omap1_spi100k {
  73. struct work_struct work;
  74. /* lock protects queue and registers */
  75. spinlock_t lock;
  76. struct list_head msg_queue;
  77. struct spi_master *master;
  78. struct clk *ick;
  79. struct clk *fck;
  80. /* Virtual base address of the controller */
  81. void __iomem *base;
  82. /* State of the SPI */
  83. unsigned int state;
  84. };
  85. struct omap1_spi100k_cs {
  86. void __iomem *base;
  87. int word_len;
  88. };
  89. static struct workqueue_struct *omap1_spi100k_wq;
  90. #define MOD_REG_BIT(val, mask, set) do { \
  91. if (set) \
  92. val |= mask; \
  93. else \
  94. val &= ~mask; \
  95. } while (0)
  96. static void spi100k_enable_clock(struct spi_master *master)
  97. {
  98. unsigned int val;
  99. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  100. /* enable SPI */
  101. val = readw(spi100k->base + SPI_SETUP1);
  102. val |= SPI_SETUP1_CLOCK_ENABLE;
  103. writew(val, spi100k->base + SPI_SETUP1);
  104. }
  105. static void spi100k_disable_clock(struct spi_master *master)
  106. {
  107. unsigned int val;
  108. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  109. /* disable SPI */
  110. val = readw(spi100k->base + SPI_SETUP1);
  111. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  112. writew(val, spi100k->base + SPI_SETUP1);
  113. }
  114. static void spi100k_write_data(struct spi_master *master, int len, int data)
  115. {
  116. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  117. /* write 16-bit word, shifting 8-bit data if necessary */
  118. if (len <= 8) {
  119. data <<= 8;
  120. len = 16;
  121. }
  122. spi100k_enable_clock(master);
  123. writew( data , spi100k->base + SPI_TX_MSB);
  124. writew(SPI_CTRL_SEN(0) |
  125. SPI_CTRL_WORD_SIZE(len) |
  126. SPI_CTRL_WR,
  127. spi100k->base + SPI_CTRL);
  128. /* Wait for bit ack send change */
  129. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE);
  130. udelay(1000);
  131. spi100k_disable_clock(master);
  132. }
  133. static int spi100k_read_data(struct spi_master *master, int len)
  134. {
  135. int dataH,dataL;
  136. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  137. /* Always do at least 16 bits */
  138. if (len <= 8)
  139. len = 16;
  140. spi100k_enable_clock(master);
  141. writew(SPI_CTRL_SEN(0) |
  142. SPI_CTRL_WORD_SIZE(len) |
  143. SPI_CTRL_RD,
  144. spi100k->base + SPI_CTRL);
  145. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD);
  146. udelay(1000);
  147. dataL = readw(spi100k->base + SPI_RX_LSB);
  148. dataH = readw(spi100k->base + SPI_RX_MSB);
  149. spi100k_disable_clock(master);
  150. return dataL;
  151. }
  152. static void spi100k_open(struct spi_master *master)
  153. {
  154. /* get control of SPI */
  155. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  156. writew(SPI_SETUP1_INT_READ_ENABLE |
  157. SPI_SETUP1_INT_WRITE_ENABLE |
  158. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  159. /* configure clock and interrupts */
  160. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  161. SPI_SETUP2_NEGATIVE_LEVEL |
  162. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  163. }
  164. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  165. {
  166. if (enable)
  167. writew(0x05fc, spi100k->base + SPI_CTRL);
  168. else
  169. writew(0x05fd, spi100k->base + SPI_CTRL);
  170. }
  171. static unsigned
  172. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  173. {
  174. struct omap1_spi100k *spi100k;
  175. struct omap1_spi100k_cs *cs = spi->controller_state;
  176. unsigned int count, c;
  177. int word_len;
  178. spi100k = spi_master_get_devdata(spi->master);
  179. count = xfer->len;
  180. c = count;
  181. word_len = cs->word_len;
  182. if (word_len <= 8) {
  183. u8 *rx;
  184. const u8 *tx;
  185. rx = xfer->rx_buf;
  186. tx = xfer->tx_buf;
  187. do {
  188. c-=1;
  189. if (xfer->tx_buf != NULL)
  190. spi100k_write_data(spi->master, word_len, *tx++);
  191. if (xfer->rx_buf != NULL)
  192. *rx++ = spi100k_read_data(spi->master, word_len);
  193. } while(c);
  194. } else if (word_len <= 16) {
  195. u16 *rx;
  196. const u16 *tx;
  197. rx = xfer->rx_buf;
  198. tx = xfer->tx_buf;
  199. do {
  200. c-=2;
  201. if (xfer->tx_buf != NULL)
  202. spi100k_write_data(spi->master,word_len, *tx++);
  203. if (xfer->rx_buf != NULL)
  204. *rx++ = spi100k_read_data(spi->master,word_len);
  205. } while(c);
  206. } else if (word_len <= 32) {
  207. u32 *rx;
  208. const u32 *tx;
  209. rx = xfer->rx_buf;
  210. tx = xfer->tx_buf;
  211. do {
  212. c-=4;
  213. if (xfer->tx_buf != NULL)
  214. spi100k_write_data(spi->master,word_len, *tx);
  215. if (xfer->rx_buf != NULL)
  216. *rx = spi100k_read_data(spi->master,word_len);
  217. } while(c);
  218. }
  219. return count - c;
  220. }
  221. /* called only when no transfer is active to this device */
  222. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  223. struct spi_transfer *t)
  224. {
  225. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  226. struct omap1_spi100k_cs *cs = spi->controller_state;
  227. u8 word_len = spi->bits_per_word;
  228. if (t != NULL && t->bits_per_word)
  229. word_len = t->bits_per_word;
  230. if (!word_len)
  231. word_len = 8;
  232. if (spi->bits_per_word > 32)
  233. return -EINVAL;
  234. cs->word_len = word_len;
  235. /* SPI init before transfer */
  236. writew(0x3e , spi100k->base + SPI_SETUP1);
  237. writew(0x00 , spi100k->base + SPI_STATUS);
  238. writew(0x3e , spi100k->base + SPI_CTRL);
  239. return 0;
  240. }
  241. /* the spi->mode bits understood by this driver: */
  242. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  243. static int omap1_spi100k_setup(struct spi_device *spi)
  244. {
  245. int ret;
  246. struct omap1_spi100k *spi100k;
  247. struct omap1_spi100k_cs *cs = spi->controller_state;
  248. spi100k = spi_master_get_devdata(spi->master);
  249. if (!cs) {
  250. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  251. if (!cs)
  252. return -ENOMEM;
  253. cs->base = spi100k->base + spi->chip_select * 0x14;
  254. spi->controller_state = cs;
  255. }
  256. spi100k_open(spi->master);
  257. clk_enable(spi100k->ick);
  258. clk_enable(spi100k->fck);
  259. ret = omap1_spi100k_setup_transfer(spi, NULL);
  260. clk_disable(spi100k->ick);
  261. clk_disable(spi100k->fck);
  262. return ret;
  263. }
  264. static void omap1_spi100k_work(struct work_struct *work)
  265. {
  266. struct omap1_spi100k *spi100k;
  267. int status = 0;
  268. spi100k = container_of(work, struct omap1_spi100k, work);
  269. spin_lock_irq(&spi100k->lock);
  270. clk_enable(spi100k->ick);
  271. clk_enable(spi100k->fck);
  272. /* We only enable one channel at a time -- the one whose message is
  273. * at the head of the queue -- although this controller would gladly
  274. * arbitrate among multiple channels. This corresponds to "single
  275. * channel" master mode. As a side effect, we need to manage the
  276. * chipselect with the FORCE bit ... CS != channel enable.
  277. */
  278. while (!list_empty(&spi100k->msg_queue)) {
  279. struct spi_message *m;
  280. struct spi_device *spi;
  281. struct spi_transfer *t = NULL;
  282. int cs_active = 0;
  283. struct omap1_spi100k_cs *cs;
  284. int par_override = 0;
  285. m = container_of(spi100k->msg_queue.next, struct spi_message,
  286. queue);
  287. list_del_init(&m->queue);
  288. spin_unlock_irq(&spi100k->lock);
  289. spi = m->spi;
  290. cs = spi->controller_state;
  291. list_for_each_entry(t, &m->transfers, transfer_list) {
  292. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  293. status = -EINVAL;
  294. break;
  295. }
  296. if (par_override || t->speed_hz || t->bits_per_word) {
  297. par_override = 1;
  298. status = omap1_spi100k_setup_transfer(spi, t);
  299. if (status < 0)
  300. break;
  301. if (!t->speed_hz && !t->bits_per_word)
  302. par_override = 0;
  303. }
  304. if (!cs_active) {
  305. omap1_spi100k_force_cs(spi100k, 1);
  306. cs_active = 1;
  307. }
  308. if (t->len) {
  309. unsigned count;
  310. count = omap1_spi100k_txrx_pio(spi, t);
  311. m->actual_length += count;
  312. if (count != t->len) {
  313. status = -EIO;
  314. break;
  315. }
  316. }
  317. if (t->delay_usecs)
  318. udelay(t->delay_usecs);
  319. /* ignore the "leave it on after last xfer" hint */
  320. if (t->cs_change) {
  321. omap1_spi100k_force_cs(spi100k, 0);
  322. cs_active = 0;
  323. }
  324. }
  325. /* Restore defaults if they were overriden */
  326. if (par_override) {
  327. par_override = 0;
  328. status = omap1_spi100k_setup_transfer(spi, NULL);
  329. }
  330. if (cs_active)
  331. omap1_spi100k_force_cs(spi100k, 0);
  332. m->status = status;
  333. m->complete(m->context);
  334. spin_lock_irq(&spi100k->lock);
  335. }
  336. clk_disable(spi100k->ick);
  337. clk_disable(spi100k->fck);
  338. spin_unlock_irq(&spi100k->lock);
  339. if (status < 0)
  340. printk(KERN_WARNING "spi transfer failed with %d\n", status);
  341. }
  342. static int omap1_spi100k_transfer(struct spi_device *spi, struct spi_message *m)
  343. {
  344. struct omap1_spi100k *spi100k;
  345. unsigned long flags;
  346. struct spi_transfer *t;
  347. m->actual_length = 0;
  348. m->status = -EINPROGRESS;
  349. spi100k = spi_master_get_devdata(spi->master);
  350. /* Don't accept new work if we're shutting down */
  351. if (spi100k->state == SPI_SHUTDOWN)
  352. return -ESHUTDOWN;
  353. /* reject invalid messages and transfers */
  354. if (list_empty(&m->transfers) || !m->complete)
  355. return -EINVAL;
  356. list_for_each_entry(t, &m->transfers, transfer_list) {
  357. const void *tx_buf = t->tx_buf;
  358. void *rx_buf = t->rx_buf;
  359. unsigned len = t->len;
  360. if (t->speed_hz > OMAP1_SPI100K_MAX_FREQ
  361. || (len && !(rx_buf || tx_buf))) {
  362. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  363. t->speed_hz,
  364. len,
  365. tx_buf ? "tx" : "",
  366. rx_buf ? "rx" : "",
  367. t->bits_per_word);
  368. return -EINVAL;
  369. }
  370. if (t->speed_hz && t->speed_hz < OMAP1_SPI100K_MAX_FREQ/(1<<16)) {
  371. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  372. t->speed_hz,
  373. OMAP1_SPI100K_MAX_FREQ/(1<<16));
  374. return -EINVAL;
  375. }
  376. }
  377. spin_lock_irqsave(&spi100k->lock, flags);
  378. list_add_tail(&m->queue, &spi100k->msg_queue);
  379. queue_work(omap1_spi100k_wq, &spi100k->work);
  380. spin_unlock_irqrestore(&spi100k->lock, flags);
  381. return 0;
  382. }
  383. static int omap1_spi100k_reset(struct omap1_spi100k *spi100k)
  384. {
  385. return 0;
  386. }
  387. static int omap1_spi100k_probe(struct platform_device *pdev)
  388. {
  389. struct spi_master *master;
  390. struct omap1_spi100k *spi100k;
  391. int status = 0;
  392. if (!pdev->id)
  393. return -EINVAL;
  394. master = spi_alloc_master(&pdev->dev, sizeof *spi100k);
  395. if (master == NULL) {
  396. dev_dbg(&pdev->dev, "master allocation failed\n");
  397. return -ENOMEM;
  398. }
  399. if (pdev->id != -1)
  400. master->bus_num = pdev->id;
  401. master->setup = omap1_spi100k_setup;
  402. master->transfer = omap1_spi100k_transfer;
  403. master->cleanup = NULL;
  404. master->num_chipselect = 2;
  405. master->mode_bits = MODEBITS;
  406. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  407. platform_set_drvdata(pdev, master);
  408. spi100k = spi_master_get_devdata(master);
  409. spi100k->master = master;
  410. /*
  411. * The memory region base address is taken as the platform_data.
  412. * You should allocate this with ioremap() before initializing
  413. * the SPI.
  414. */
  415. spi100k->base = (void __iomem *) pdev->dev.platform_data;
  416. INIT_WORK(&spi100k->work, omap1_spi100k_work);
  417. spin_lock_init(&spi100k->lock);
  418. INIT_LIST_HEAD(&spi100k->msg_queue);
  419. spi100k->ick = clk_get(&pdev->dev, "ick");
  420. if (IS_ERR(spi100k->ick)) {
  421. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  422. status = PTR_ERR(spi100k->ick);
  423. goto err1;
  424. }
  425. spi100k->fck = clk_get(&pdev->dev, "fck");
  426. if (IS_ERR(spi100k->fck)) {
  427. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  428. status = PTR_ERR(spi100k->fck);
  429. goto err2;
  430. }
  431. if (omap1_spi100k_reset(spi100k) < 0)
  432. goto err3;
  433. status = spi_register_master(master);
  434. if (status < 0)
  435. goto err3;
  436. spi100k->state = SPI_RUNNING;
  437. return status;
  438. err3:
  439. clk_put(spi100k->fck);
  440. err2:
  441. clk_put(spi100k->ick);
  442. err1:
  443. spi_master_put(master);
  444. return status;
  445. }
  446. static int omap1_spi100k_remove(struct platform_device *pdev)
  447. {
  448. struct spi_master *master;
  449. struct omap1_spi100k *spi100k;
  450. struct resource *r;
  451. unsigned limit = 500;
  452. unsigned long flags;
  453. int status = 0;
  454. master = platform_get_drvdata(pdev);
  455. spi100k = spi_master_get_devdata(master);
  456. spin_lock_irqsave(&spi100k->lock, flags);
  457. spi100k->state = SPI_SHUTDOWN;
  458. while (!list_empty(&spi100k->msg_queue) && limit--) {
  459. spin_unlock_irqrestore(&spi100k->lock, flags);
  460. msleep(10);
  461. spin_lock_irqsave(&spi100k->lock, flags);
  462. }
  463. if (!list_empty(&spi100k->msg_queue))
  464. status = -EBUSY;
  465. spin_unlock_irqrestore(&spi100k->lock, flags);
  466. if (status != 0)
  467. return status;
  468. clk_put(spi100k->fck);
  469. clk_put(spi100k->ick);
  470. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  471. spi_unregister_master(master);
  472. return 0;
  473. }
  474. static struct platform_driver omap1_spi100k_driver = {
  475. .driver = {
  476. .name = "omap1_spi100k",
  477. .owner = THIS_MODULE,
  478. },
  479. .remove = omap1_spi100k_remove,
  480. };
  481. static int __init omap1_spi100k_init(void)
  482. {
  483. omap1_spi100k_wq = create_singlethread_workqueue(
  484. omap1_spi100k_driver.driver.name);
  485. if (omap1_spi100k_wq == NULL)
  486. return -1;
  487. return platform_driver_probe(&omap1_spi100k_driver, omap1_spi100k_probe);
  488. }
  489. static void __exit omap1_spi100k_exit(void)
  490. {
  491. platform_driver_unregister(&omap1_spi100k_driver);
  492. destroy_workqueue(omap1_spi100k_wq);
  493. }
  494. module_init(omap1_spi100k_init);
  495. module_exit(omap1_spi100k_exit);
  496. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  497. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  498. MODULE_LICENSE("GPL");