spi-omap-100k.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  249. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  250. spi->bits_per_word);
  251. return -EINVAL;
  252. }
  253. spi100k = spi_master_get_devdata(spi->master);
  254. if (!cs) {
  255. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  256. if (!cs)
  257. return -ENOMEM;
  258. cs->base = spi100k->base + spi->chip_select * 0x14;
  259. spi->controller_state = cs;
  260. }
  261. spi100k_open(spi->master);
  262. clk_enable(spi100k->ick);
  263. clk_enable(spi100k->fck);
  264. ret = omap1_spi100k_setup_transfer(spi, NULL);
  265. clk_disable(spi100k->ick);
  266. clk_disable(spi100k->fck);
  267. return ret;
  268. }
  269. static void omap1_spi100k_work(struct work_struct *work)
  270. {
  271. struct omap1_spi100k *spi100k;
  272. int status = 0;
  273. spi100k = container_of(work, struct omap1_spi100k, work);
  274. spin_lock_irq(&spi100k->lock);
  275. clk_enable(spi100k->ick);
  276. clk_enable(spi100k->fck);
  277. /* We only enable one channel at a time -- the one whose message is
  278. * at the head of the queue -- although this controller would gladly
  279. * arbitrate among multiple channels. This corresponds to "single
  280. * channel" master mode. As a side effect, we need to manage the
  281. * chipselect with the FORCE bit ... CS != channel enable.
  282. */
  283. while (!list_empty(&spi100k->msg_queue)) {
  284. struct spi_message *m;
  285. struct spi_device *spi;
  286. struct spi_transfer *t = NULL;
  287. int cs_active = 0;
  288. struct omap1_spi100k_cs *cs;
  289. int par_override = 0;
  290. m = container_of(spi100k->msg_queue.next, struct spi_message,
  291. queue);
  292. list_del_init(&m->queue);
  293. spin_unlock_irq(&spi100k->lock);
  294. spi = m->spi;
  295. cs = spi->controller_state;
  296. list_for_each_entry(t, &m->transfers, transfer_list) {
  297. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  298. status = -EINVAL;
  299. break;
  300. }
  301. if (par_override || t->speed_hz || t->bits_per_word) {
  302. par_override = 1;
  303. status = omap1_spi100k_setup_transfer(spi, t);
  304. if (status < 0)
  305. break;
  306. if (!t->speed_hz && !t->bits_per_word)
  307. par_override = 0;
  308. }
  309. if (!cs_active) {
  310. omap1_spi100k_force_cs(spi100k, 1);
  311. cs_active = 1;
  312. }
  313. if (t->len) {
  314. unsigned count;
  315. count = omap1_spi100k_txrx_pio(spi, t);
  316. m->actual_length += count;
  317. if (count != t->len) {
  318. status = -EIO;
  319. break;
  320. }
  321. }
  322. if (t->delay_usecs)
  323. udelay(t->delay_usecs);
  324. /* ignore the "leave it on after last xfer" hint */
  325. if (t->cs_change) {
  326. omap1_spi100k_force_cs(spi100k, 0);
  327. cs_active = 0;
  328. }
  329. }
  330. /* Restore defaults if they were overriden */
  331. if (par_override) {
  332. par_override = 0;
  333. status = omap1_spi100k_setup_transfer(spi, NULL);
  334. }
  335. if (cs_active)
  336. omap1_spi100k_force_cs(spi100k, 0);
  337. m->status = status;
  338. m->complete(m->context);
  339. spin_lock_irq(&spi100k->lock);
  340. }
  341. clk_disable(spi100k->ick);
  342. clk_disable(spi100k->fck);
  343. spin_unlock_irq(&spi100k->lock);
  344. if (status < 0)
  345. printk(KERN_WARNING "spi transfer failed with %d\n", status);
  346. }
  347. static int omap1_spi100k_transfer(struct spi_device *spi, struct spi_message *m)
  348. {
  349. struct omap1_spi100k *spi100k;
  350. unsigned long flags;
  351. struct spi_transfer *t;
  352. m->actual_length = 0;
  353. m->status = -EINPROGRESS;
  354. spi100k = spi_master_get_devdata(spi->master);
  355. /* Don't accept new work if we're shutting down */
  356. if (spi100k->state == SPI_SHUTDOWN)
  357. return -ESHUTDOWN;
  358. /* reject invalid messages and transfers */
  359. if (list_empty(&m->transfers) || !m->complete)
  360. return -EINVAL;
  361. list_for_each_entry(t, &m->transfers, transfer_list) {
  362. const void *tx_buf = t->tx_buf;
  363. void *rx_buf = t->rx_buf;
  364. unsigned len = t->len;
  365. if (t->speed_hz > OMAP1_SPI100K_MAX_FREQ
  366. || (len && !(rx_buf || tx_buf))
  367. || (t->bits_per_word &&
  368. ( t->bits_per_word < 4
  369. || t->bits_per_word > 32))) {
  370. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  371. t->speed_hz,
  372. len,
  373. tx_buf ? "tx" : "",
  374. rx_buf ? "rx" : "",
  375. t->bits_per_word);
  376. return -EINVAL;
  377. }
  378. if (t->speed_hz && t->speed_hz < OMAP1_SPI100K_MAX_FREQ/(1<<16)) {
  379. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  380. t->speed_hz,
  381. OMAP1_SPI100K_MAX_FREQ/(1<<16));
  382. return -EINVAL;
  383. }
  384. }
  385. spin_lock_irqsave(&spi100k->lock, flags);
  386. list_add_tail(&m->queue, &spi100k->msg_queue);
  387. queue_work(omap1_spi100k_wq, &spi100k->work);
  388. spin_unlock_irqrestore(&spi100k->lock, flags);
  389. return 0;
  390. }
  391. static int omap1_spi100k_reset(struct omap1_spi100k *spi100k)
  392. {
  393. return 0;
  394. }
  395. static int omap1_spi100k_probe(struct platform_device *pdev)
  396. {
  397. struct spi_master *master;
  398. struct omap1_spi100k *spi100k;
  399. int status = 0;
  400. if (!pdev->id)
  401. return -EINVAL;
  402. master = spi_alloc_master(&pdev->dev, sizeof *spi100k);
  403. if (master == NULL) {
  404. dev_dbg(&pdev->dev, "master allocation failed\n");
  405. return -ENOMEM;
  406. }
  407. if (pdev->id != -1)
  408. master->bus_num = pdev->id;
  409. master->setup = omap1_spi100k_setup;
  410. master->transfer = omap1_spi100k_transfer;
  411. master->cleanup = NULL;
  412. master->num_chipselect = 2;
  413. master->mode_bits = MODEBITS;
  414. dev_set_drvdata(&pdev->dev, master);
  415. spi100k = spi_master_get_devdata(master);
  416. spi100k->master = master;
  417. /*
  418. * The memory region base address is taken as the platform_data.
  419. * You should allocate this with ioremap() before initializing
  420. * the SPI.
  421. */
  422. spi100k->base = (void __iomem *) pdev->dev.platform_data;
  423. INIT_WORK(&spi100k->work, omap1_spi100k_work);
  424. spin_lock_init(&spi100k->lock);
  425. INIT_LIST_HEAD(&spi100k->msg_queue);
  426. spi100k->ick = clk_get(&pdev->dev, "ick");
  427. if (IS_ERR(spi100k->ick)) {
  428. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  429. status = PTR_ERR(spi100k->ick);
  430. goto err1;
  431. }
  432. spi100k->fck = clk_get(&pdev->dev, "fck");
  433. if (IS_ERR(spi100k->fck)) {
  434. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  435. status = PTR_ERR(spi100k->fck);
  436. goto err2;
  437. }
  438. if (omap1_spi100k_reset(spi100k) < 0)
  439. goto err3;
  440. status = spi_register_master(master);
  441. if (status < 0)
  442. goto err3;
  443. spi100k->state = SPI_RUNNING;
  444. return status;
  445. err3:
  446. clk_put(spi100k->fck);
  447. err2:
  448. clk_put(spi100k->ick);
  449. err1:
  450. spi_master_put(master);
  451. return status;
  452. }
  453. static int omap1_spi100k_remove(struct platform_device *pdev)
  454. {
  455. struct spi_master *master;
  456. struct omap1_spi100k *spi100k;
  457. struct resource *r;
  458. unsigned limit = 500;
  459. unsigned long flags;
  460. int status = 0;
  461. master = dev_get_drvdata(&pdev->dev);
  462. spi100k = spi_master_get_devdata(master);
  463. spin_lock_irqsave(&spi100k->lock, flags);
  464. spi100k->state = SPI_SHUTDOWN;
  465. while (!list_empty(&spi100k->msg_queue) && limit--) {
  466. spin_unlock_irqrestore(&spi100k->lock, flags);
  467. msleep(10);
  468. spin_lock_irqsave(&spi100k->lock, flags);
  469. }
  470. if (!list_empty(&spi100k->msg_queue))
  471. status = -EBUSY;
  472. spin_unlock_irqrestore(&spi100k->lock, flags);
  473. if (status != 0)
  474. return status;
  475. clk_put(spi100k->fck);
  476. clk_put(spi100k->ick);
  477. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  478. spi_unregister_master(master);
  479. return 0;
  480. }
  481. static struct platform_driver omap1_spi100k_driver = {
  482. .driver = {
  483. .name = "omap1_spi100k",
  484. .owner = THIS_MODULE,
  485. },
  486. .remove = omap1_spi100k_remove,
  487. };
  488. static int __init omap1_spi100k_init(void)
  489. {
  490. omap1_spi100k_wq = create_singlethread_workqueue(
  491. omap1_spi100k_driver.driver.name);
  492. if (omap1_spi100k_wq == NULL)
  493. return -1;
  494. return platform_driver_probe(&omap1_spi100k_driver, omap1_spi100k_probe);
  495. }
  496. static void __exit omap1_spi100k_exit(void)
  497. {
  498. platform_driver_unregister(&omap1_spi100k_driver);
  499. destroy_workqueue(omap1_spi100k_wq);
  500. }
  501. module_init(omap1_spi100k_init);
  502. module_exit(omap1_spi100k_exit);
  503. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  504. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  505. MODULE_LICENSE("GPL");