omap2_mcspi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrjölä <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/spi/spi.h>
  35. #include <mach/dma.h>
  36. #include <mach/clock.h>
  37. #define OMAP2_MCSPI_MAX_FREQ 48000000
  38. #define OMAP2_MCSPI_REVISION 0x00
  39. #define OMAP2_MCSPI_SYSCONFIG 0x10
  40. #define OMAP2_MCSPI_SYSSTATUS 0x14
  41. #define OMAP2_MCSPI_IRQSTATUS 0x18
  42. #define OMAP2_MCSPI_IRQENABLE 0x1c
  43. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  44. #define OMAP2_MCSPI_SYST 0x24
  45. #define OMAP2_MCSPI_MODULCTRL 0x28
  46. /* per-channel banks, 0x14 bytes each, first is: */
  47. #define OMAP2_MCSPI_CHCONF0 0x2c
  48. #define OMAP2_MCSPI_CHSTAT0 0x30
  49. #define OMAP2_MCSPI_CHCTRL0 0x34
  50. #define OMAP2_MCSPI_TX0 0x38
  51. #define OMAP2_MCSPI_RX0 0x3c
  52. /* per-register bitmasks: */
  53. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE (1 << 0)
  54. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET (1 << 1)
  55. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE (1 << 0)
  56. #define OMAP2_MCSPI_MODULCTRL_SINGLE (1 << 0)
  57. #define OMAP2_MCSPI_MODULCTRL_MS (1 << 2)
  58. #define OMAP2_MCSPI_MODULCTRL_STEST (1 << 3)
  59. #define OMAP2_MCSPI_CHCONF_PHA (1 << 0)
  60. #define OMAP2_MCSPI_CHCONF_POL (1 << 1)
  61. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  62. #define OMAP2_MCSPI_CHCONF_EPOL (1 << 6)
  63. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  64. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY (0x01 << 12)
  65. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY (0x02 << 12)
  66. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  67. #define OMAP2_MCSPI_CHCONF_DMAW (1 << 14)
  68. #define OMAP2_MCSPI_CHCONF_DMAR (1 << 15)
  69. #define OMAP2_MCSPI_CHCONF_DPE0 (1 << 16)
  70. #define OMAP2_MCSPI_CHCONF_DPE1 (1 << 17)
  71. #define OMAP2_MCSPI_CHCONF_IS (1 << 18)
  72. #define OMAP2_MCSPI_CHCONF_TURBO (1 << 19)
  73. #define OMAP2_MCSPI_CHCONF_FORCE (1 << 20)
  74. #define OMAP2_MCSPI_CHSTAT_RXS (1 << 0)
  75. #define OMAP2_MCSPI_CHSTAT_TXS (1 << 1)
  76. #define OMAP2_MCSPI_CHSTAT_EOT (1 << 2)
  77. #define OMAP2_MCSPI_CHCTRL_EN (1 << 0)
  78. /* We have 2 DMA channels per CS, one for RX and one for TX */
  79. struct omap2_mcspi_dma {
  80. int dma_tx_channel;
  81. int dma_rx_channel;
  82. int dma_tx_sync_dev;
  83. int dma_rx_sync_dev;
  84. struct completion dma_tx_completion;
  85. struct completion dma_rx_completion;
  86. };
  87. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  88. * cache operations; better heuristics consider wordsize and bitrate.
  89. */
  90. #define DMA_MIN_BYTES 8
  91. struct omap2_mcspi {
  92. struct work_struct work;
  93. /* lock protects queue and registers */
  94. spinlock_t lock;
  95. struct list_head msg_queue;
  96. struct spi_master *master;
  97. struct clk *ick;
  98. struct clk *fck;
  99. /* Virtual base address of the controller */
  100. void __iomem *base;
  101. /* SPI1 has 4 channels, while SPI2 has 2 */
  102. struct omap2_mcspi_dma *dma_channels;
  103. };
  104. struct omap2_mcspi_cs {
  105. void __iomem *base;
  106. int word_len;
  107. };
  108. static struct workqueue_struct *omap2_mcspi_wq;
  109. #define MOD_REG_BIT(val, mask, set) do { \
  110. if (set) \
  111. val |= mask; \
  112. else \
  113. val &= ~mask; \
  114. } while (0)
  115. static inline void mcspi_write_reg(struct spi_master *master,
  116. int idx, u32 val)
  117. {
  118. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  119. __raw_writel(val, mcspi->base + idx);
  120. }
  121. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  122. {
  123. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  124. return __raw_readl(mcspi->base + idx);
  125. }
  126. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  127. int idx, u32 val)
  128. {
  129. struct omap2_mcspi_cs *cs = spi->controller_state;
  130. __raw_writel(val, cs->base + idx);
  131. }
  132. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  133. {
  134. struct omap2_mcspi_cs *cs = spi->controller_state;
  135. return __raw_readl(cs->base + idx);
  136. }
  137. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  138. int is_read, int enable)
  139. {
  140. u32 l, rw;
  141. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  142. if (is_read) /* 1 is read, 0 write */
  143. rw = OMAP2_MCSPI_CHCONF_DMAR;
  144. else
  145. rw = OMAP2_MCSPI_CHCONF_DMAW;
  146. MOD_REG_BIT(l, rw, enable);
  147. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  148. }
  149. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  150. {
  151. u32 l;
  152. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  153. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  154. }
  155. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  156. {
  157. u32 l;
  158. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  159. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  160. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  161. }
  162. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  163. {
  164. u32 l;
  165. /* setup when switching from (reset default) slave mode
  166. * to single-channel master mode
  167. */
  168. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  169. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  170. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  171. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  172. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  173. }
  174. static unsigned
  175. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  176. {
  177. struct omap2_mcspi *mcspi;
  178. struct omap2_mcspi_cs *cs = spi->controller_state;
  179. struct omap2_mcspi_dma *mcspi_dma;
  180. unsigned int count, c;
  181. unsigned long base, tx_reg, rx_reg;
  182. int word_len, data_type, element_count;
  183. u8 * rx;
  184. const u8 * tx;
  185. mcspi = spi_master_get_devdata(spi->master);
  186. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  187. count = xfer->len;
  188. c = count;
  189. word_len = cs->word_len;
  190. base = (unsigned long) io_v2p(cs->base);
  191. tx_reg = base + OMAP2_MCSPI_TX0;
  192. rx_reg = base + OMAP2_MCSPI_RX0;
  193. rx = xfer->rx_buf;
  194. tx = xfer->tx_buf;
  195. if (word_len <= 8) {
  196. data_type = OMAP_DMA_DATA_TYPE_S8;
  197. element_count = count;
  198. } else if (word_len <= 16) {
  199. data_type = OMAP_DMA_DATA_TYPE_S16;
  200. element_count = count >> 1;
  201. } else /* word_len <= 32 */ {
  202. data_type = OMAP_DMA_DATA_TYPE_S32;
  203. element_count = count >> 2;
  204. }
  205. if (tx != NULL) {
  206. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  207. data_type, element_count, 1,
  208. OMAP_DMA_SYNC_ELEMENT,
  209. mcspi_dma->dma_tx_sync_dev, 0);
  210. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  211. OMAP_DMA_AMODE_CONSTANT,
  212. tx_reg, 0, 0);
  213. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  214. OMAP_DMA_AMODE_POST_INC,
  215. xfer->tx_dma, 0, 0);
  216. }
  217. if (rx != NULL) {
  218. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  219. data_type, element_count, 1,
  220. OMAP_DMA_SYNC_ELEMENT,
  221. mcspi_dma->dma_rx_sync_dev, 1);
  222. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  223. OMAP_DMA_AMODE_CONSTANT,
  224. rx_reg, 0, 0);
  225. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  226. OMAP_DMA_AMODE_POST_INC,
  227. xfer->rx_dma, 0, 0);
  228. }
  229. if (tx != NULL) {
  230. omap_start_dma(mcspi_dma->dma_tx_channel);
  231. omap2_mcspi_set_dma_req(spi, 0, 1);
  232. }
  233. if (rx != NULL) {
  234. omap_start_dma(mcspi_dma->dma_rx_channel);
  235. omap2_mcspi_set_dma_req(spi, 1, 1);
  236. }
  237. if (tx != NULL) {
  238. wait_for_completion(&mcspi_dma->dma_tx_completion);
  239. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  240. }
  241. if (rx != NULL) {
  242. wait_for_completion(&mcspi_dma->dma_rx_completion);
  243. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  244. }
  245. return count;
  246. }
  247. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  248. {
  249. unsigned long timeout;
  250. timeout = jiffies + msecs_to_jiffies(1000);
  251. while (!(__raw_readl(reg) & bit)) {
  252. if (time_after(jiffies, timeout))
  253. return -1;
  254. cpu_relax();
  255. }
  256. return 0;
  257. }
  258. static unsigned
  259. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  260. {
  261. struct omap2_mcspi *mcspi;
  262. struct omap2_mcspi_cs *cs = spi->controller_state;
  263. unsigned int count, c;
  264. u32 l;
  265. void __iomem *base = cs->base;
  266. void __iomem *tx_reg;
  267. void __iomem *rx_reg;
  268. void __iomem *chstat_reg;
  269. int word_len;
  270. mcspi = spi_master_get_devdata(spi->master);
  271. count = xfer->len;
  272. c = count;
  273. word_len = cs->word_len;
  274. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  275. l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  276. /* We store the pre-calculated register addresses on stack to speed
  277. * up the transfer loop. */
  278. tx_reg = base + OMAP2_MCSPI_TX0;
  279. rx_reg = base + OMAP2_MCSPI_RX0;
  280. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  281. if (word_len <= 8) {
  282. u8 *rx;
  283. const u8 *tx;
  284. rx = xfer->rx_buf;
  285. tx = xfer->tx_buf;
  286. do {
  287. c -= 1;
  288. if (tx != NULL) {
  289. if (mcspi_wait_for_reg_bit(chstat_reg,
  290. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  291. dev_err(&spi->dev, "TXS timed out\n");
  292. goto out;
  293. }
  294. #ifdef VERBOSE
  295. dev_dbg(&spi->dev, "write-%d %02x\n",
  296. word_len, *tx);
  297. #endif
  298. __raw_writel(*tx++, tx_reg);
  299. }
  300. if (rx != NULL) {
  301. if (mcspi_wait_for_reg_bit(chstat_reg,
  302. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  303. dev_err(&spi->dev, "RXS timed out\n");
  304. goto out;
  305. }
  306. /* prevent last RX_ONLY read from triggering
  307. * more word i/o: switch to rx+tx
  308. */
  309. if (c == 0 && tx == NULL)
  310. mcspi_write_cs_reg(spi,
  311. OMAP2_MCSPI_CHCONF0, l);
  312. *rx++ = __raw_readl(rx_reg);
  313. #ifdef VERBOSE
  314. dev_dbg(&spi->dev, "read-%d %02x\n",
  315. word_len, *(rx - 1));
  316. #endif
  317. }
  318. } while (c);
  319. } else if (word_len <= 16) {
  320. u16 *rx;
  321. const u16 *tx;
  322. rx = xfer->rx_buf;
  323. tx = xfer->tx_buf;
  324. do {
  325. c -= 2;
  326. if (tx != NULL) {
  327. if (mcspi_wait_for_reg_bit(chstat_reg,
  328. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  329. dev_err(&spi->dev, "TXS timed out\n");
  330. goto out;
  331. }
  332. #ifdef VERBOSE
  333. dev_dbg(&spi->dev, "write-%d %04x\n",
  334. word_len, *tx);
  335. #endif
  336. __raw_writel(*tx++, tx_reg);
  337. }
  338. if (rx != NULL) {
  339. if (mcspi_wait_for_reg_bit(chstat_reg,
  340. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  341. dev_err(&spi->dev, "RXS timed out\n");
  342. goto out;
  343. }
  344. /* prevent last RX_ONLY read from triggering
  345. * more word i/o: switch to rx+tx
  346. */
  347. if (c == 0 && tx == NULL)
  348. mcspi_write_cs_reg(spi,
  349. OMAP2_MCSPI_CHCONF0, l);
  350. *rx++ = __raw_readl(rx_reg);
  351. #ifdef VERBOSE
  352. dev_dbg(&spi->dev, "read-%d %04x\n",
  353. word_len, *(rx - 1));
  354. #endif
  355. }
  356. } while (c);
  357. } else if (word_len <= 32) {
  358. u32 *rx;
  359. const u32 *tx;
  360. rx = xfer->rx_buf;
  361. tx = xfer->tx_buf;
  362. do {
  363. c -= 4;
  364. if (tx != NULL) {
  365. if (mcspi_wait_for_reg_bit(chstat_reg,
  366. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  367. dev_err(&spi->dev, "TXS timed out\n");
  368. goto out;
  369. }
  370. #ifdef VERBOSE
  371. dev_dbg(&spi->dev, "write-%d %04x\n",
  372. word_len, *tx);
  373. #endif
  374. __raw_writel(*tx++, tx_reg);
  375. }
  376. if (rx != NULL) {
  377. if (mcspi_wait_for_reg_bit(chstat_reg,
  378. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  379. dev_err(&spi->dev, "RXS timed out\n");
  380. goto out;
  381. }
  382. /* prevent last RX_ONLY read from triggering
  383. * more word i/o: switch to rx+tx
  384. */
  385. if (c == 0 && tx == NULL)
  386. mcspi_write_cs_reg(spi,
  387. OMAP2_MCSPI_CHCONF0, l);
  388. *rx++ = __raw_readl(rx_reg);
  389. #ifdef VERBOSE
  390. dev_dbg(&spi->dev, "read-%d %04x\n",
  391. word_len, *(rx - 1));
  392. #endif
  393. }
  394. } while (c);
  395. }
  396. /* for TX_ONLY mode, be sure all words have shifted out */
  397. if (xfer->rx_buf == NULL) {
  398. if (mcspi_wait_for_reg_bit(chstat_reg,
  399. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  400. dev_err(&spi->dev, "TXS timed out\n");
  401. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  402. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  403. dev_err(&spi->dev, "EOT timed out\n");
  404. }
  405. out:
  406. return count - c;
  407. }
  408. /* called only when no transfer is active to this device */
  409. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  410. struct spi_transfer *t)
  411. {
  412. struct omap2_mcspi_cs *cs = spi->controller_state;
  413. struct omap2_mcspi *mcspi;
  414. u32 l = 0, div = 0;
  415. u8 word_len = spi->bits_per_word;
  416. mcspi = spi_master_get_devdata(spi->master);
  417. if (t != NULL && t->bits_per_word)
  418. word_len = t->bits_per_word;
  419. cs->word_len = word_len;
  420. if (spi->max_speed_hz) {
  421. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  422. > spi->max_speed_hz)
  423. div++;
  424. } else
  425. div = 15;
  426. l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  427. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  428. * REVISIT: this controller could support SPI_3WIRE mode.
  429. */
  430. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  431. l |= OMAP2_MCSPI_CHCONF_DPE0;
  432. /* wordlength */
  433. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  434. l |= (word_len - 1) << 7;
  435. /* set chipselect polarity; manage with FORCE */
  436. if (!(spi->mode & SPI_CS_HIGH))
  437. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  438. else
  439. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  440. /* set clock divisor */
  441. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  442. l |= div << 2;
  443. /* set SPI mode 0..3 */
  444. if (spi->mode & SPI_CPOL)
  445. l |= OMAP2_MCSPI_CHCONF_POL;
  446. else
  447. l &= ~OMAP2_MCSPI_CHCONF_POL;
  448. if (spi->mode & SPI_CPHA)
  449. l |= OMAP2_MCSPI_CHCONF_PHA;
  450. else
  451. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  452. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
  453. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  454. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  455. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  456. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  457. return 0;
  458. }
  459. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  460. {
  461. struct spi_device *spi = data;
  462. struct omap2_mcspi *mcspi;
  463. struct omap2_mcspi_dma *mcspi_dma;
  464. mcspi = spi_master_get_devdata(spi->master);
  465. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  466. complete(&mcspi_dma->dma_rx_completion);
  467. /* We must disable the DMA RX request */
  468. omap2_mcspi_set_dma_req(spi, 1, 0);
  469. }
  470. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  471. {
  472. struct spi_device *spi = data;
  473. struct omap2_mcspi *mcspi;
  474. struct omap2_mcspi_dma *mcspi_dma;
  475. mcspi = spi_master_get_devdata(spi->master);
  476. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  477. complete(&mcspi_dma->dma_tx_completion);
  478. /* We must disable the DMA TX request */
  479. omap2_mcspi_set_dma_req(spi, 0, 0);
  480. }
  481. static int omap2_mcspi_request_dma(struct spi_device *spi)
  482. {
  483. struct spi_master *master = spi->master;
  484. struct omap2_mcspi *mcspi;
  485. struct omap2_mcspi_dma *mcspi_dma;
  486. mcspi = spi_master_get_devdata(master);
  487. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  488. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  489. omap2_mcspi_dma_rx_callback, spi,
  490. &mcspi_dma->dma_rx_channel)) {
  491. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  492. return -EAGAIN;
  493. }
  494. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  495. omap2_mcspi_dma_tx_callback, spi,
  496. &mcspi_dma->dma_tx_channel)) {
  497. omap_free_dma(mcspi_dma->dma_rx_channel);
  498. mcspi_dma->dma_rx_channel = -1;
  499. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  500. return -EAGAIN;
  501. }
  502. init_completion(&mcspi_dma->dma_rx_completion);
  503. init_completion(&mcspi_dma->dma_tx_completion);
  504. return 0;
  505. }
  506. /* the spi->mode bits understood by this driver: */
  507. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  508. static int omap2_mcspi_setup(struct spi_device *spi)
  509. {
  510. int ret;
  511. struct omap2_mcspi *mcspi;
  512. struct omap2_mcspi_dma *mcspi_dma;
  513. struct omap2_mcspi_cs *cs = spi->controller_state;
  514. if (spi->mode & ~MODEBITS) {
  515. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  516. spi->mode & ~MODEBITS);
  517. return -EINVAL;
  518. }
  519. if (spi->bits_per_word == 0)
  520. spi->bits_per_word = 8;
  521. else if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  522. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  523. spi->bits_per_word);
  524. return -EINVAL;
  525. }
  526. mcspi = spi_master_get_devdata(spi->master);
  527. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  528. if (!cs) {
  529. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  530. if (!cs)
  531. return -ENOMEM;
  532. cs->base = mcspi->base + spi->chip_select * 0x14;
  533. spi->controller_state = cs;
  534. }
  535. if (mcspi_dma->dma_rx_channel == -1
  536. || mcspi_dma->dma_tx_channel == -1) {
  537. ret = omap2_mcspi_request_dma(spi);
  538. if (ret < 0)
  539. return ret;
  540. }
  541. clk_enable(mcspi->ick);
  542. clk_enable(mcspi->fck);
  543. ret = omap2_mcspi_setup_transfer(spi, NULL);
  544. clk_disable(mcspi->fck);
  545. clk_disable(mcspi->ick);
  546. return ret;
  547. }
  548. static void omap2_mcspi_cleanup(struct spi_device *spi)
  549. {
  550. struct omap2_mcspi *mcspi;
  551. struct omap2_mcspi_dma *mcspi_dma;
  552. mcspi = spi_master_get_devdata(spi->master);
  553. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  554. kfree(spi->controller_state);
  555. if (mcspi_dma->dma_rx_channel != -1) {
  556. omap_free_dma(mcspi_dma->dma_rx_channel);
  557. mcspi_dma->dma_rx_channel = -1;
  558. }
  559. if (mcspi_dma->dma_tx_channel != -1) {
  560. omap_free_dma(mcspi_dma->dma_tx_channel);
  561. mcspi_dma->dma_tx_channel = -1;
  562. }
  563. }
  564. static void omap2_mcspi_work(struct work_struct *work)
  565. {
  566. struct omap2_mcspi *mcspi;
  567. mcspi = container_of(work, struct omap2_mcspi, work);
  568. spin_lock_irq(&mcspi->lock);
  569. clk_enable(mcspi->ick);
  570. clk_enable(mcspi->fck);
  571. /* We only enable one channel at a time -- the one whose message is
  572. * at the head of the queue -- although this controller would gladly
  573. * arbitrate among multiple channels. This corresponds to "single
  574. * channel" master mode. As a side effect, we need to manage the
  575. * chipselect with the FORCE bit ... CS != channel enable.
  576. */
  577. while (!list_empty(&mcspi->msg_queue)) {
  578. struct spi_message *m;
  579. struct spi_device *spi;
  580. struct spi_transfer *t = NULL;
  581. int cs_active = 0;
  582. struct omap2_mcspi_cs *cs;
  583. int par_override = 0;
  584. int status = 0;
  585. u32 chconf;
  586. m = container_of(mcspi->msg_queue.next, struct spi_message,
  587. queue);
  588. list_del_init(&m->queue);
  589. spin_unlock_irq(&mcspi->lock);
  590. spi = m->spi;
  591. cs = spi->controller_state;
  592. omap2_mcspi_set_enable(spi, 1);
  593. list_for_each_entry(t, &m->transfers, transfer_list) {
  594. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  595. status = -EINVAL;
  596. break;
  597. }
  598. if (par_override || t->speed_hz || t->bits_per_word) {
  599. par_override = 1;
  600. status = omap2_mcspi_setup_transfer(spi, t);
  601. if (status < 0)
  602. break;
  603. if (!t->speed_hz && !t->bits_per_word)
  604. par_override = 0;
  605. }
  606. if (!cs_active) {
  607. omap2_mcspi_force_cs(spi, 1);
  608. cs_active = 1;
  609. }
  610. chconf = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  611. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  612. if (t->tx_buf == NULL)
  613. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  614. else if (t->rx_buf == NULL)
  615. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  616. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, chconf);
  617. if (t->len) {
  618. unsigned count;
  619. /* RX_ONLY mode needs dummy data in TX reg */
  620. if (t->tx_buf == NULL)
  621. __raw_writel(0, cs->base
  622. + OMAP2_MCSPI_TX0);
  623. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  624. count = omap2_mcspi_txrx_dma(spi, t);
  625. else
  626. count = omap2_mcspi_txrx_pio(spi, t);
  627. m->actual_length += count;
  628. if (count != t->len) {
  629. status = -EIO;
  630. break;
  631. }
  632. }
  633. if (t->delay_usecs)
  634. udelay(t->delay_usecs);
  635. /* ignore the "leave it on after last xfer" hint */
  636. if (t->cs_change) {
  637. omap2_mcspi_force_cs(spi, 0);
  638. cs_active = 0;
  639. }
  640. }
  641. /* Restore defaults if they were overriden */
  642. if (par_override) {
  643. par_override = 0;
  644. status = omap2_mcspi_setup_transfer(spi, NULL);
  645. }
  646. if (cs_active)
  647. omap2_mcspi_force_cs(spi, 0);
  648. omap2_mcspi_set_enable(spi, 0);
  649. m->status = status;
  650. m->complete(m->context);
  651. spin_lock_irq(&mcspi->lock);
  652. }
  653. clk_disable(mcspi->fck);
  654. clk_disable(mcspi->ick);
  655. spin_unlock_irq(&mcspi->lock);
  656. }
  657. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  658. {
  659. struct omap2_mcspi *mcspi;
  660. unsigned long flags;
  661. struct spi_transfer *t;
  662. m->actual_length = 0;
  663. m->status = 0;
  664. /* reject invalid messages and transfers */
  665. if (list_empty(&m->transfers) || !m->complete)
  666. return -EINVAL;
  667. list_for_each_entry(t, &m->transfers, transfer_list) {
  668. const void *tx_buf = t->tx_buf;
  669. void *rx_buf = t->rx_buf;
  670. unsigned len = t->len;
  671. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  672. || (len && !(rx_buf || tx_buf))
  673. || (t->bits_per_word &&
  674. ( t->bits_per_word < 4
  675. || t->bits_per_word > 32))) {
  676. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  677. t->speed_hz,
  678. len,
  679. tx_buf ? "tx" : "",
  680. rx_buf ? "rx" : "",
  681. t->bits_per_word);
  682. return -EINVAL;
  683. }
  684. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  685. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  686. t->speed_hz,
  687. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  688. return -EINVAL;
  689. }
  690. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  691. continue;
  692. /* Do DMA mapping "early" for better error reporting and
  693. * dcache use. Note that if dma_unmap_single() ever starts
  694. * to do real work on ARM, we'd need to clean up mappings
  695. * for previous transfers on *ALL* exits of this loop...
  696. */
  697. if (tx_buf != NULL) {
  698. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  699. len, DMA_TO_DEVICE);
  700. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  701. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  702. 'T', len);
  703. return -EINVAL;
  704. }
  705. }
  706. if (rx_buf != NULL) {
  707. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  708. DMA_FROM_DEVICE);
  709. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  710. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  711. 'R', len);
  712. if (tx_buf != NULL)
  713. dma_unmap_single(NULL, t->tx_dma,
  714. len, DMA_TO_DEVICE);
  715. return -EINVAL;
  716. }
  717. }
  718. }
  719. mcspi = spi_master_get_devdata(spi->master);
  720. spin_lock_irqsave(&mcspi->lock, flags);
  721. list_add_tail(&m->queue, &mcspi->msg_queue);
  722. queue_work(omap2_mcspi_wq, &mcspi->work);
  723. spin_unlock_irqrestore(&mcspi->lock, flags);
  724. return 0;
  725. }
  726. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  727. {
  728. struct spi_master *master = mcspi->master;
  729. u32 tmp;
  730. clk_enable(mcspi->ick);
  731. clk_enable(mcspi->fck);
  732. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  733. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  734. do {
  735. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  736. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  737. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  738. /* (3 << 8) | (2 << 3) | */
  739. OMAP2_MCSPI_SYSCONFIG_AUTOIDLE);
  740. omap2_mcspi_set_master_mode(master);
  741. clk_disable(mcspi->fck);
  742. clk_disable(mcspi->ick);
  743. return 0;
  744. }
  745. static u8 __initdata spi1_rxdma_id [] = {
  746. OMAP24XX_DMA_SPI1_RX0,
  747. OMAP24XX_DMA_SPI1_RX1,
  748. OMAP24XX_DMA_SPI1_RX2,
  749. OMAP24XX_DMA_SPI1_RX3,
  750. };
  751. static u8 __initdata spi1_txdma_id [] = {
  752. OMAP24XX_DMA_SPI1_TX0,
  753. OMAP24XX_DMA_SPI1_TX1,
  754. OMAP24XX_DMA_SPI1_TX2,
  755. OMAP24XX_DMA_SPI1_TX3,
  756. };
  757. static u8 __initdata spi2_rxdma_id[] = {
  758. OMAP24XX_DMA_SPI2_RX0,
  759. OMAP24XX_DMA_SPI2_RX1,
  760. };
  761. static u8 __initdata spi2_txdma_id[] = {
  762. OMAP24XX_DMA_SPI2_TX0,
  763. OMAP24XX_DMA_SPI2_TX1,
  764. };
  765. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
  766. static u8 __initdata spi3_rxdma_id[] = {
  767. OMAP24XX_DMA_SPI3_RX0,
  768. OMAP24XX_DMA_SPI3_RX1,
  769. };
  770. static u8 __initdata spi3_txdma_id[] = {
  771. OMAP24XX_DMA_SPI3_TX0,
  772. OMAP24XX_DMA_SPI3_TX1,
  773. };
  774. #endif
  775. #ifdef CONFIG_ARCH_OMAP3
  776. static u8 __initdata spi4_rxdma_id[] = {
  777. OMAP34XX_DMA_SPI4_RX0,
  778. };
  779. static u8 __initdata spi4_txdma_id[] = {
  780. OMAP34XX_DMA_SPI4_TX0,
  781. };
  782. #endif
  783. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  784. {
  785. struct spi_master *master;
  786. struct omap2_mcspi *mcspi;
  787. struct resource *r;
  788. int status = 0, i;
  789. const u8 *rxdma_id, *txdma_id;
  790. unsigned num_chipselect;
  791. switch (pdev->id) {
  792. case 1:
  793. rxdma_id = spi1_rxdma_id;
  794. txdma_id = spi1_txdma_id;
  795. num_chipselect = 4;
  796. break;
  797. case 2:
  798. rxdma_id = spi2_rxdma_id;
  799. txdma_id = spi2_txdma_id;
  800. num_chipselect = 2;
  801. break;
  802. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
  803. case 3:
  804. rxdma_id = spi3_rxdma_id;
  805. txdma_id = spi3_txdma_id;
  806. num_chipselect = 2;
  807. break;
  808. #endif
  809. #ifdef CONFIG_ARCH_OMAP3
  810. case 4:
  811. rxdma_id = spi4_rxdma_id;
  812. txdma_id = spi4_txdma_id;
  813. num_chipselect = 1;
  814. break;
  815. #endif
  816. default:
  817. return -EINVAL;
  818. }
  819. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  820. if (master == NULL) {
  821. dev_dbg(&pdev->dev, "master allocation failed\n");
  822. return -ENOMEM;
  823. }
  824. if (pdev->id != -1)
  825. master->bus_num = pdev->id;
  826. master->setup = omap2_mcspi_setup;
  827. master->transfer = omap2_mcspi_transfer;
  828. master->cleanup = omap2_mcspi_cleanup;
  829. master->num_chipselect = num_chipselect;
  830. dev_set_drvdata(&pdev->dev, master);
  831. mcspi = spi_master_get_devdata(master);
  832. mcspi->master = master;
  833. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  834. if (r == NULL) {
  835. status = -ENODEV;
  836. goto err1;
  837. }
  838. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  839. pdev->dev.bus_id)) {
  840. status = -EBUSY;
  841. goto err1;
  842. }
  843. mcspi->base = (void __iomem *) io_p2v(r->start);
  844. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  845. spin_lock_init(&mcspi->lock);
  846. INIT_LIST_HEAD(&mcspi->msg_queue);
  847. mcspi->ick = clk_get(&pdev->dev, "mcspi_ick");
  848. if (IS_ERR(mcspi->ick)) {
  849. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  850. status = PTR_ERR(mcspi->ick);
  851. goto err1a;
  852. }
  853. mcspi->fck = clk_get(&pdev->dev, "mcspi_fck");
  854. if (IS_ERR(mcspi->fck)) {
  855. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  856. status = PTR_ERR(mcspi->fck);
  857. goto err2;
  858. }
  859. mcspi->dma_channels = kcalloc(master->num_chipselect,
  860. sizeof(struct omap2_mcspi_dma),
  861. GFP_KERNEL);
  862. if (mcspi->dma_channels == NULL)
  863. goto err3;
  864. for (i = 0; i < num_chipselect; i++) {
  865. mcspi->dma_channels[i].dma_rx_channel = -1;
  866. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  867. mcspi->dma_channels[i].dma_tx_channel = -1;
  868. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  869. }
  870. if (omap2_mcspi_reset(mcspi) < 0)
  871. goto err4;
  872. status = spi_register_master(master);
  873. if (status < 0)
  874. goto err4;
  875. return status;
  876. err4:
  877. kfree(mcspi->dma_channels);
  878. err3:
  879. clk_put(mcspi->fck);
  880. err2:
  881. clk_put(mcspi->ick);
  882. err1a:
  883. release_mem_region(r->start, (r->end - r->start) + 1);
  884. err1:
  885. spi_master_put(master);
  886. return status;
  887. }
  888. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  889. {
  890. struct spi_master *master;
  891. struct omap2_mcspi *mcspi;
  892. struct omap2_mcspi_dma *dma_channels;
  893. struct resource *r;
  894. master = dev_get_drvdata(&pdev->dev);
  895. mcspi = spi_master_get_devdata(master);
  896. dma_channels = mcspi->dma_channels;
  897. clk_put(mcspi->fck);
  898. clk_put(mcspi->ick);
  899. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  900. release_mem_region(r->start, (r->end - r->start) + 1);
  901. spi_unregister_master(master);
  902. kfree(dma_channels);
  903. return 0;
  904. }
  905. /* work with hotplug and coldplug */
  906. MODULE_ALIAS("platform:omap2_mcspi");
  907. static struct platform_driver omap2_mcspi_driver = {
  908. .driver = {
  909. .name = "omap2_mcspi",
  910. .owner = THIS_MODULE,
  911. },
  912. .remove = __exit_p(omap2_mcspi_remove),
  913. };
  914. static int __init omap2_mcspi_init(void)
  915. {
  916. omap2_mcspi_wq = create_singlethread_workqueue(
  917. omap2_mcspi_driver.driver.name);
  918. if (omap2_mcspi_wq == NULL)
  919. return -1;
  920. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  921. }
  922. subsys_initcall(omap2_mcspi_init);
  923. static void __exit omap2_mcspi_exit(void)
  924. {
  925. platform_driver_unregister(&omap2_mcspi_driver);
  926. destroy_workqueue(omap2_mcspi_wq);
  927. }
  928. module_exit(omap2_mcspi_exit);
  929. MODULE_LICENSE("GPL");