spi-omap-100k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 spi_master *master;
  74. struct clk *ick;
  75. struct clk *fck;
  76. /* Virtual base address of the controller */
  77. void __iomem *base;
  78. /* State of the SPI */
  79. unsigned int state;
  80. };
  81. struct omap1_spi100k_cs {
  82. void __iomem *base;
  83. int word_len;
  84. };
  85. #define MOD_REG_BIT(val, mask, set) do { \
  86. if (set) \
  87. val |= mask; \
  88. else \
  89. val &= ~mask; \
  90. } while (0)
  91. static void spi100k_enable_clock(struct spi_master *master)
  92. {
  93. unsigned int val;
  94. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  95. /* enable SPI */
  96. val = readw(spi100k->base + SPI_SETUP1);
  97. val |= SPI_SETUP1_CLOCK_ENABLE;
  98. writew(val, spi100k->base + SPI_SETUP1);
  99. }
  100. static void spi100k_disable_clock(struct spi_master *master)
  101. {
  102. unsigned int val;
  103. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  104. /* disable SPI */
  105. val = readw(spi100k->base + SPI_SETUP1);
  106. val &= ~SPI_SETUP1_CLOCK_ENABLE;
  107. writew(val, spi100k->base + SPI_SETUP1);
  108. }
  109. static void spi100k_write_data(struct spi_master *master, int len, int data)
  110. {
  111. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  112. /* write 16-bit word, shifting 8-bit data if necessary */
  113. if (len <= 8) {
  114. data <<= 8;
  115. len = 16;
  116. }
  117. spi100k_enable_clock(master);
  118. writew( data , spi100k->base + SPI_TX_MSB);
  119. writew(SPI_CTRL_SEN(0) |
  120. SPI_CTRL_WORD_SIZE(len) |
  121. SPI_CTRL_WR,
  122. spi100k->base + SPI_CTRL);
  123. /* Wait for bit ack send change */
  124. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE);
  125. udelay(1000);
  126. spi100k_disable_clock(master);
  127. }
  128. static int spi100k_read_data(struct spi_master *master, int len)
  129. {
  130. int dataH,dataL;
  131. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  132. /* Always do at least 16 bits */
  133. if (len <= 8)
  134. len = 16;
  135. spi100k_enable_clock(master);
  136. writew(SPI_CTRL_SEN(0) |
  137. SPI_CTRL_WORD_SIZE(len) |
  138. SPI_CTRL_RD,
  139. spi100k->base + SPI_CTRL);
  140. while((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD);
  141. udelay(1000);
  142. dataL = readw(spi100k->base + SPI_RX_LSB);
  143. dataH = readw(spi100k->base + SPI_RX_MSB);
  144. spi100k_disable_clock(master);
  145. return dataL;
  146. }
  147. static void spi100k_open(struct spi_master *master)
  148. {
  149. /* get control of SPI */
  150. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  151. writew(SPI_SETUP1_INT_READ_ENABLE |
  152. SPI_SETUP1_INT_WRITE_ENABLE |
  153. SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1);
  154. /* configure clock and interrupts */
  155. writew(SPI_SETUP2_ACTIVE_EDGE_FALLING |
  156. SPI_SETUP2_NEGATIVE_LEVEL |
  157. SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2);
  158. }
  159. static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable)
  160. {
  161. if (enable)
  162. writew(0x05fc, spi100k->base + SPI_CTRL);
  163. else
  164. writew(0x05fd, spi100k->base + SPI_CTRL);
  165. }
  166. static unsigned
  167. omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  168. {
  169. struct omap1_spi100k *spi100k;
  170. struct omap1_spi100k_cs *cs = spi->controller_state;
  171. unsigned int count, c;
  172. int word_len;
  173. spi100k = spi_master_get_devdata(spi->master);
  174. count = xfer->len;
  175. c = count;
  176. word_len = cs->word_len;
  177. if (word_len <= 8) {
  178. u8 *rx;
  179. const u8 *tx;
  180. rx = xfer->rx_buf;
  181. tx = xfer->tx_buf;
  182. do {
  183. c-=1;
  184. if (xfer->tx_buf != NULL)
  185. spi100k_write_data(spi->master, word_len, *tx++);
  186. if (xfer->rx_buf != NULL)
  187. *rx++ = spi100k_read_data(spi->master, word_len);
  188. } while(c);
  189. } else if (word_len <= 16) {
  190. u16 *rx;
  191. const u16 *tx;
  192. rx = xfer->rx_buf;
  193. tx = xfer->tx_buf;
  194. do {
  195. c-=2;
  196. if (xfer->tx_buf != NULL)
  197. spi100k_write_data(spi->master,word_len, *tx++);
  198. if (xfer->rx_buf != NULL)
  199. *rx++ = spi100k_read_data(spi->master,word_len);
  200. } while(c);
  201. } else if (word_len <= 32) {
  202. u32 *rx;
  203. const u32 *tx;
  204. rx = xfer->rx_buf;
  205. tx = xfer->tx_buf;
  206. do {
  207. c-=4;
  208. if (xfer->tx_buf != NULL)
  209. spi100k_write_data(spi->master,word_len, *tx);
  210. if (xfer->rx_buf != NULL)
  211. *rx = spi100k_read_data(spi->master,word_len);
  212. } while(c);
  213. }
  214. return count - c;
  215. }
  216. /* called only when no transfer is active to this device */
  217. static int omap1_spi100k_setup_transfer(struct spi_device *spi,
  218. struct spi_transfer *t)
  219. {
  220. struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master);
  221. struct omap1_spi100k_cs *cs = spi->controller_state;
  222. u8 word_len = spi->bits_per_word;
  223. if (t != NULL && t->bits_per_word)
  224. word_len = t->bits_per_word;
  225. if (!word_len)
  226. word_len = 8;
  227. if (spi->bits_per_word > 32)
  228. return -EINVAL;
  229. cs->word_len = word_len;
  230. /* SPI init before transfer */
  231. writew(0x3e , spi100k->base + SPI_SETUP1);
  232. writew(0x00 , spi100k->base + SPI_STATUS);
  233. writew(0x3e , spi100k->base + SPI_CTRL);
  234. return 0;
  235. }
  236. /* the spi->mode bits understood by this driver: */
  237. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  238. static int omap1_spi100k_setup(struct spi_device *spi)
  239. {
  240. int ret;
  241. struct omap1_spi100k *spi100k;
  242. struct omap1_spi100k_cs *cs = spi->controller_state;
  243. spi100k = spi_master_get_devdata(spi->master);
  244. if (!cs) {
  245. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  246. if (!cs)
  247. return -ENOMEM;
  248. cs->base = spi100k->base + spi->chip_select * 0x14;
  249. spi->controller_state = cs;
  250. }
  251. spi100k_open(spi->master);
  252. clk_prepare_enable(spi100k->ick);
  253. clk_prepare_enable(spi100k->fck);
  254. ret = omap1_spi100k_setup_transfer(spi, NULL);
  255. clk_disable_unprepare(spi100k->ick);
  256. clk_disable_unprepare(spi100k->fck);
  257. return ret;
  258. }
  259. static int omap1_spi100k_prepare_hardware(struct spi_master *master)
  260. {
  261. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  262. clk_prepare_enable(spi100k->ick);
  263. clk_prepare_enable(spi100k->fck);
  264. return 0;
  265. }
  266. static int omap1_spi100k_transfer_one_message(struct spi_master *master,
  267. struct spi_message *m)
  268. {
  269. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  270. struct spi_device *spi = m->spi;
  271. struct spi_transfer *t = NULL;
  272. int cs_active = 0;
  273. int par_override = 0;
  274. int status = 0;
  275. list_for_each_entry(t, &m->transfers, transfer_list) {
  276. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  277. status = -EINVAL;
  278. break;
  279. }
  280. if (par_override || t->speed_hz || t->bits_per_word) {
  281. par_override = 1;
  282. status = omap1_spi100k_setup_transfer(spi, t);
  283. if (status < 0)
  284. break;
  285. if (!t->speed_hz && !t->bits_per_word)
  286. par_override = 0;
  287. }
  288. if (!cs_active) {
  289. omap1_spi100k_force_cs(spi100k, 1);
  290. cs_active = 1;
  291. }
  292. if (t->len) {
  293. unsigned count;
  294. count = omap1_spi100k_txrx_pio(spi, t);
  295. m->actual_length += count;
  296. if (count != t->len) {
  297. status = -EIO;
  298. break;
  299. }
  300. }
  301. if (t->delay_usecs)
  302. udelay(t->delay_usecs);
  303. /* ignore the "leave it on after last xfer" hint */
  304. if (t->cs_change) {
  305. omap1_spi100k_force_cs(spi100k, 0);
  306. cs_active = 0;
  307. }
  308. }
  309. /* Restore defaults if they were overriden */
  310. if (par_override) {
  311. par_override = 0;
  312. status = omap1_spi100k_setup_transfer(spi, NULL);
  313. }
  314. if (cs_active)
  315. omap1_spi100k_force_cs(spi100k, 0);
  316. m->status = status;
  317. spi_finalize_current_message(master);
  318. return status;
  319. }
  320. static int omap1_spi100k_unprepare_hardware(struct spi_master *master)
  321. {
  322. struct omap1_spi100k *spi100k = spi_master_get_devdata(master);
  323. clk_disable_unprepare(spi100k->ick);
  324. clk_disable_unprepare(spi100k->fck);
  325. return 0;
  326. }
  327. static int omap1_spi100k_probe(struct platform_device *pdev)
  328. {
  329. struct spi_master *master;
  330. struct omap1_spi100k *spi100k;
  331. int status = 0;
  332. if (!pdev->id)
  333. return -EINVAL;
  334. master = spi_alloc_master(&pdev->dev, sizeof *spi100k);
  335. if (master == NULL) {
  336. dev_dbg(&pdev->dev, "master allocation failed\n");
  337. return -ENOMEM;
  338. }
  339. if (pdev->id != -1)
  340. master->bus_num = pdev->id;
  341. master->setup = omap1_spi100k_setup;
  342. master->transfer_one_message = omap1_spi100k_transfer_one_message;
  343. master->prepare_transfer_hardware = omap1_spi100k_prepare_hardware;
  344. master->unprepare_transfer_hardware = omap1_spi100k_unprepare_hardware;
  345. master->cleanup = NULL;
  346. master->num_chipselect = 2;
  347. master->mode_bits = MODEBITS;
  348. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  349. master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16);
  350. master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ;
  351. platform_set_drvdata(pdev, master);
  352. spi100k = spi_master_get_devdata(master);
  353. spi100k->master = master;
  354. /*
  355. * The memory region base address is taken as the platform_data.
  356. * You should allocate this with ioremap() before initializing
  357. * the SPI.
  358. */
  359. spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev);
  360. spi100k->ick = devm_clk_get(&pdev->dev, "ick");
  361. if (IS_ERR(spi100k->ick)) {
  362. dev_dbg(&pdev->dev, "can't get spi100k_ick\n");
  363. status = PTR_ERR(spi100k->ick);
  364. goto err;
  365. }
  366. spi100k->fck = devm_clk_get(&pdev->dev, "fck");
  367. if (IS_ERR(spi100k->fck)) {
  368. dev_dbg(&pdev->dev, "can't get spi100k_fck\n");
  369. status = PTR_ERR(spi100k->fck);
  370. goto err;
  371. }
  372. status = spi_register_master(master);
  373. if (status < 0)
  374. goto err;
  375. spi100k->state = SPI_RUNNING;
  376. return status;
  377. err:
  378. spi_master_put(master);
  379. return status;
  380. }
  381. static int omap1_spi100k_remove(struct platform_device *pdev)
  382. {
  383. struct spi_master *master;
  384. struct omap1_spi100k *spi100k;
  385. struct resource *r;
  386. int status = 0;
  387. master = platform_get_drvdata(pdev);
  388. spi100k = spi_master_get_devdata(master);
  389. if (status != 0)
  390. return status;
  391. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  392. spi_unregister_master(master);
  393. return 0;
  394. }
  395. static struct platform_driver omap1_spi100k_driver = {
  396. .driver = {
  397. .name = "omap1_spi100k",
  398. .owner = THIS_MODULE,
  399. },
  400. .probe = omap1_spi100k_probe,
  401. .remove = omap1_spi100k_remove,
  402. };
  403. module_platform_driver(omap1_spi100k_driver);
  404. MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver");
  405. MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>");
  406. MODULE_LICENSE("GPL");