spi-omap-100k.c 16 KB

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