spi-omap2-mcspi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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/dmaengine.h>
  31. #include <linux/omap-dma.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/err.h>
  34. #include <linux/clk.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/of.h>
  39. #include <linux/of_device.h>
  40. #include <linux/pinctrl/consumer.h>
  41. #include <linux/spi/spi.h>
  42. #include <linux/platform_data/spi-omap2-mcspi.h>
  43. #define OMAP2_MCSPI_MAX_FREQ 48000000
  44. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  45. #define OMAP2_MCSPI_REVISION 0x00
  46. #define OMAP2_MCSPI_SYSSTATUS 0x14
  47. #define OMAP2_MCSPI_IRQSTATUS 0x18
  48. #define OMAP2_MCSPI_IRQENABLE 0x1c
  49. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  50. #define OMAP2_MCSPI_SYST 0x24
  51. #define OMAP2_MCSPI_MODULCTRL 0x28
  52. /* per-channel banks, 0x14 bytes each, first is: */
  53. #define OMAP2_MCSPI_CHCONF0 0x2c
  54. #define OMAP2_MCSPI_CHSTAT0 0x30
  55. #define OMAP2_MCSPI_CHCTRL0 0x34
  56. #define OMAP2_MCSPI_TX0 0x38
  57. #define OMAP2_MCSPI_RX0 0x3c
  58. /* per-register bitmasks: */
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  78. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  79. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  80. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  81. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  82. /* We have 2 DMA channels per CS, one for RX and one for TX */
  83. struct omap2_mcspi_dma {
  84. struct dma_chan *dma_tx;
  85. struct dma_chan *dma_rx;
  86. int dma_tx_sync_dev;
  87. int dma_rx_sync_dev;
  88. struct completion dma_tx_completion;
  89. struct completion dma_rx_completion;
  90. };
  91. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  92. * cache operations; better heuristics consider wordsize and bitrate.
  93. */
  94. #define DMA_MIN_BYTES 160
  95. /*
  96. * Used for context save and restore, structure members to be updated whenever
  97. * corresponding registers are modified.
  98. */
  99. struct omap2_mcspi_regs {
  100. u32 modulctrl;
  101. u32 wakeupenable;
  102. struct list_head cs;
  103. };
  104. struct omap2_mcspi {
  105. struct spi_master *master;
  106. /* Virtual base address of the controller */
  107. void __iomem *base;
  108. unsigned long phys;
  109. /* SPI1 has 4 channels, while SPI2 has 2 */
  110. struct omap2_mcspi_dma *dma_channels;
  111. struct device *dev;
  112. struct omap2_mcspi_regs ctx;
  113. unsigned int pin_dir:1;
  114. };
  115. struct omap2_mcspi_cs {
  116. void __iomem *base;
  117. unsigned long phys;
  118. int word_len;
  119. struct list_head node;
  120. /* Context save and restore shadow register */
  121. u32 chconf0;
  122. };
  123. static inline void mcspi_write_reg(struct spi_master *master,
  124. int idx, u32 val)
  125. {
  126. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  127. __raw_writel(val, mcspi->base + idx);
  128. }
  129. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  130. {
  131. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  132. return __raw_readl(mcspi->base + idx);
  133. }
  134. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  135. int idx, u32 val)
  136. {
  137. struct omap2_mcspi_cs *cs = spi->controller_state;
  138. __raw_writel(val, cs->base + idx);
  139. }
  140. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  141. {
  142. struct omap2_mcspi_cs *cs = spi->controller_state;
  143. return __raw_readl(cs->base + idx);
  144. }
  145. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  146. {
  147. struct omap2_mcspi_cs *cs = spi->controller_state;
  148. return cs->chconf0;
  149. }
  150. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  151. {
  152. struct omap2_mcspi_cs *cs = spi->controller_state;
  153. cs->chconf0 = val;
  154. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  155. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  156. }
  157. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  158. int is_read, int enable)
  159. {
  160. u32 l, rw;
  161. l = mcspi_cached_chconf0(spi);
  162. if (is_read) /* 1 is read, 0 write */
  163. rw = OMAP2_MCSPI_CHCONF_DMAR;
  164. else
  165. rw = OMAP2_MCSPI_CHCONF_DMAW;
  166. if (enable)
  167. l |= rw;
  168. else
  169. l &= ~rw;
  170. mcspi_write_chconf0(spi, l);
  171. }
  172. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  173. {
  174. u32 l;
  175. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  176. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  177. /* Flash post-writes */
  178. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  179. }
  180. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  181. {
  182. u32 l;
  183. l = mcspi_cached_chconf0(spi);
  184. if (cs_active)
  185. l |= OMAP2_MCSPI_CHCONF_FORCE;
  186. else
  187. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  188. mcspi_write_chconf0(spi, l);
  189. }
  190. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  191. {
  192. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  193. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  194. u32 l;
  195. /*
  196. * Setup when switching from (reset default) slave mode
  197. * to single-channel master mode
  198. */
  199. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  200. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  201. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  202. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  203. ctx->modulctrl = l;
  204. }
  205. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  206. {
  207. struct spi_master *spi_cntrl = mcspi->master;
  208. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  209. struct omap2_mcspi_cs *cs;
  210. /* McSPI: context restore */
  211. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  212. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  213. list_for_each_entry(cs, &ctx->cs, node)
  214. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  215. }
  216. static int omap2_prepare_transfer(struct spi_master *master)
  217. {
  218. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  219. pm_runtime_get_sync(mcspi->dev);
  220. return 0;
  221. }
  222. static int omap2_unprepare_transfer(struct spi_master *master)
  223. {
  224. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  225. pm_runtime_mark_last_busy(mcspi->dev);
  226. pm_runtime_put_autosuspend(mcspi->dev);
  227. return 0;
  228. }
  229. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  230. {
  231. unsigned long timeout;
  232. timeout = jiffies + msecs_to_jiffies(1000);
  233. while (!(__raw_readl(reg) & bit)) {
  234. if (time_after(jiffies, timeout))
  235. return -1;
  236. cpu_relax();
  237. }
  238. return 0;
  239. }
  240. static void omap2_mcspi_rx_callback(void *data)
  241. {
  242. struct spi_device *spi = data;
  243. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  244. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  245. complete(&mcspi_dma->dma_rx_completion);
  246. /* We must disable the DMA RX request */
  247. omap2_mcspi_set_dma_req(spi, 1, 0);
  248. }
  249. static void omap2_mcspi_tx_callback(void *data)
  250. {
  251. struct spi_device *spi = data;
  252. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  253. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  254. complete(&mcspi_dma->dma_tx_completion);
  255. /* We must disable the DMA TX request */
  256. omap2_mcspi_set_dma_req(spi, 0, 0);
  257. }
  258. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  259. struct spi_transfer *xfer,
  260. struct dma_slave_config cfg)
  261. {
  262. struct omap2_mcspi *mcspi;
  263. struct omap2_mcspi_dma *mcspi_dma;
  264. unsigned int count;
  265. mcspi = spi_master_get_devdata(spi->master);
  266. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  267. count = xfer->len;
  268. if (mcspi_dma->dma_tx) {
  269. struct dma_async_tx_descriptor *tx;
  270. struct scatterlist sg;
  271. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  272. sg_init_table(&sg, 1);
  273. sg_dma_address(&sg) = xfer->tx_dma;
  274. sg_dma_len(&sg) = xfer->len;
  275. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
  276. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  277. if (tx) {
  278. tx->callback = omap2_mcspi_tx_callback;
  279. tx->callback_param = spi;
  280. dmaengine_submit(tx);
  281. } else {
  282. /* FIXME: fall back to PIO? */
  283. }
  284. }
  285. dma_async_issue_pending(mcspi_dma->dma_tx);
  286. omap2_mcspi_set_dma_req(spi, 0, 1);
  287. }
  288. static unsigned
  289. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  290. struct dma_slave_config cfg,
  291. unsigned es)
  292. {
  293. struct omap2_mcspi *mcspi;
  294. struct omap2_mcspi_dma *mcspi_dma;
  295. unsigned int count;
  296. u32 l;
  297. int elements = 0;
  298. int word_len, element_count;
  299. struct omap2_mcspi_cs *cs = spi->controller_state;
  300. mcspi = spi_master_get_devdata(spi->master);
  301. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  302. count = xfer->len;
  303. word_len = cs->word_len;
  304. l = mcspi_cached_chconf0(spi);
  305. if (word_len <= 8)
  306. element_count = count;
  307. else if (word_len <= 16)
  308. element_count = count >> 1;
  309. else /* word_len <= 32 */
  310. element_count = count >> 2;
  311. if (mcspi_dma->dma_rx) {
  312. struct dma_async_tx_descriptor *tx;
  313. struct scatterlist sg;
  314. size_t len = xfer->len - es;
  315. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  316. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  317. len -= es;
  318. sg_init_table(&sg, 1);
  319. sg_dma_address(&sg) = xfer->rx_dma;
  320. sg_dma_len(&sg) = len;
  321. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
  322. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
  323. DMA_CTRL_ACK);
  324. if (tx) {
  325. tx->callback = omap2_mcspi_rx_callback;
  326. tx->callback_param = spi;
  327. dmaengine_submit(tx);
  328. } else {
  329. /* FIXME: fall back to PIO? */
  330. }
  331. }
  332. dma_async_issue_pending(mcspi_dma->dma_rx);
  333. omap2_mcspi_set_dma_req(spi, 1, 1);
  334. wait_for_completion(&mcspi_dma->dma_rx_completion);
  335. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  336. DMA_FROM_DEVICE);
  337. omap2_mcspi_set_enable(spi, 0);
  338. elements = element_count - 1;
  339. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  340. elements--;
  341. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  342. & OMAP2_MCSPI_CHSTAT_RXS)) {
  343. u32 w;
  344. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  345. if (word_len <= 8)
  346. ((u8 *)xfer->rx_buf)[elements++] = w;
  347. else if (word_len <= 16)
  348. ((u16 *)xfer->rx_buf)[elements++] = w;
  349. else /* word_len <= 32 */
  350. ((u32 *)xfer->rx_buf)[elements++] = w;
  351. } else {
  352. dev_err(&spi->dev, "DMA RX penultimate word empty");
  353. count -= (word_len <= 8) ? 2 :
  354. (word_len <= 16) ? 4 :
  355. /* word_len <= 32 */ 8;
  356. omap2_mcspi_set_enable(spi, 1);
  357. return count;
  358. }
  359. }
  360. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  361. & OMAP2_MCSPI_CHSTAT_RXS)) {
  362. u32 w;
  363. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  364. if (word_len <= 8)
  365. ((u8 *)xfer->rx_buf)[elements] = w;
  366. else if (word_len <= 16)
  367. ((u16 *)xfer->rx_buf)[elements] = w;
  368. else /* word_len <= 32 */
  369. ((u32 *)xfer->rx_buf)[elements] = w;
  370. } else {
  371. dev_err(&spi->dev, "DMA RX last word empty");
  372. count -= (word_len <= 8) ? 1 :
  373. (word_len <= 16) ? 2 :
  374. /* word_len <= 32 */ 4;
  375. }
  376. omap2_mcspi_set_enable(spi, 1);
  377. return count;
  378. }
  379. static unsigned
  380. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  381. {
  382. struct omap2_mcspi *mcspi;
  383. struct omap2_mcspi_cs *cs = spi->controller_state;
  384. struct omap2_mcspi_dma *mcspi_dma;
  385. unsigned int count;
  386. u32 l;
  387. u8 *rx;
  388. const u8 *tx;
  389. struct dma_slave_config cfg;
  390. enum dma_slave_buswidth width;
  391. unsigned es;
  392. void __iomem *chstat_reg;
  393. mcspi = spi_master_get_devdata(spi->master);
  394. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  395. l = mcspi_cached_chconf0(spi);
  396. if (cs->word_len <= 8) {
  397. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  398. es = 1;
  399. } else if (cs->word_len <= 16) {
  400. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  401. es = 2;
  402. } else {
  403. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  404. es = 4;
  405. }
  406. memset(&cfg, 0, sizeof(cfg));
  407. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  408. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  409. cfg.src_addr_width = width;
  410. cfg.dst_addr_width = width;
  411. cfg.src_maxburst = 1;
  412. cfg.dst_maxburst = 1;
  413. rx = xfer->rx_buf;
  414. tx = xfer->tx_buf;
  415. count = xfer->len;
  416. if (tx != NULL)
  417. omap2_mcspi_tx_dma(spi, xfer, cfg);
  418. if (rx != NULL)
  419. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  420. if (tx != NULL) {
  421. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  422. wait_for_completion(&mcspi_dma->dma_tx_completion);
  423. dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
  424. DMA_TO_DEVICE);
  425. /* for TX_ONLY mode, be sure all words have shifted out */
  426. if (rx == NULL) {
  427. if (mcspi_wait_for_reg_bit(chstat_reg,
  428. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  429. dev_err(&spi->dev, "TXS timed out\n");
  430. else if (mcspi_wait_for_reg_bit(chstat_reg,
  431. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  432. dev_err(&spi->dev, "EOT timed out\n");
  433. }
  434. }
  435. return count;
  436. }
  437. static unsigned
  438. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  439. {
  440. struct omap2_mcspi *mcspi;
  441. struct omap2_mcspi_cs *cs = spi->controller_state;
  442. unsigned int count, c;
  443. u32 l;
  444. void __iomem *base = cs->base;
  445. void __iomem *tx_reg;
  446. void __iomem *rx_reg;
  447. void __iomem *chstat_reg;
  448. int word_len;
  449. mcspi = spi_master_get_devdata(spi->master);
  450. count = xfer->len;
  451. c = count;
  452. word_len = cs->word_len;
  453. l = mcspi_cached_chconf0(spi);
  454. /* We store the pre-calculated register addresses on stack to speed
  455. * up the transfer loop. */
  456. tx_reg = base + OMAP2_MCSPI_TX0;
  457. rx_reg = base + OMAP2_MCSPI_RX0;
  458. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  459. if (c < (word_len>>3))
  460. return 0;
  461. if (word_len <= 8) {
  462. u8 *rx;
  463. const u8 *tx;
  464. rx = xfer->rx_buf;
  465. tx = xfer->tx_buf;
  466. do {
  467. c -= 1;
  468. if (tx != NULL) {
  469. if (mcspi_wait_for_reg_bit(chstat_reg,
  470. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  471. dev_err(&spi->dev, "TXS timed out\n");
  472. goto out;
  473. }
  474. dev_vdbg(&spi->dev, "write-%d %02x\n",
  475. word_len, *tx);
  476. __raw_writel(*tx++, tx_reg);
  477. }
  478. if (rx != NULL) {
  479. if (mcspi_wait_for_reg_bit(chstat_reg,
  480. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  481. dev_err(&spi->dev, "RXS timed out\n");
  482. goto out;
  483. }
  484. if (c == 1 && tx == NULL &&
  485. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  486. omap2_mcspi_set_enable(spi, 0);
  487. *rx++ = __raw_readl(rx_reg);
  488. dev_vdbg(&spi->dev, "read-%d %02x\n",
  489. word_len, *(rx - 1));
  490. if (mcspi_wait_for_reg_bit(chstat_reg,
  491. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  492. dev_err(&spi->dev,
  493. "RXS timed out\n");
  494. goto out;
  495. }
  496. c = 0;
  497. } else if (c == 0 && tx == NULL) {
  498. omap2_mcspi_set_enable(spi, 0);
  499. }
  500. *rx++ = __raw_readl(rx_reg);
  501. dev_vdbg(&spi->dev, "read-%d %02x\n",
  502. word_len, *(rx - 1));
  503. }
  504. } while (c);
  505. } else if (word_len <= 16) {
  506. u16 *rx;
  507. const u16 *tx;
  508. rx = xfer->rx_buf;
  509. tx = xfer->tx_buf;
  510. do {
  511. c -= 2;
  512. if (tx != NULL) {
  513. if (mcspi_wait_for_reg_bit(chstat_reg,
  514. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  515. dev_err(&spi->dev, "TXS timed out\n");
  516. goto out;
  517. }
  518. dev_vdbg(&spi->dev, "write-%d %04x\n",
  519. word_len, *tx);
  520. __raw_writel(*tx++, tx_reg);
  521. }
  522. if (rx != NULL) {
  523. if (mcspi_wait_for_reg_bit(chstat_reg,
  524. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  525. dev_err(&spi->dev, "RXS timed out\n");
  526. goto out;
  527. }
  528. if (c == 2 && tx == NULL &&
  529. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  530. omap2_mcspi_set_enable(spi, 0);
  531. *rx++ = __raw_readl(rx_reg);
  532. dev_vdbg(&spi->dev, "read-%d %04x\n",
  533. word_len, *(rx - 1));
  534. if (mcspi_wait_for_reg_bit(chstat_reg,
  535. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  536. dev_err(&spi->dev,
  537. "RXS timed out\n");
  538. goto out;
  539. }
  540. c = 0;
  541. } else if (c == 0 && tx == NULL) {
  542. omap2_mcspi_set_enable(spi, 0);
  543. }
  544. *rx++ = __raw_readl(rx_reg);
  545. dev_vdbg(&spi->dev, "read-%d %04x\n",
  546. word_len, *(rx - 1));
  547. }
  548. } while (c >= 2);
  549. } else if (word_len <= 32) {
  550. u32 *rx;
  551. const u32 *tx;
  552. rx = xfer->rx_buf;
  553. tx = xfer->tx_buf;
  554. do {
  555. c -= 4;
  556. if (tx != NULL) {
  557. if (mcspi_wait_for_reg_bit(chstat_reg,
  558. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  559. dev_err(&spi->dev, "TXS timed out\n");
  560. goto out;
  561. }
  562. dev_vdbg(&spi->dev, "write-%d %08x\n",
  563. word_len, *tx);
  564. __raw_writel(*tx++, tx_reg);
  565. }
  566. if (rx != NULL) {
  567. if (mcspi_wait_for_reg_bit(chstat_reg,
  568. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  569. dev_err(&spi->dev, "RXS timed out\n");
  570. goto out;
  571. }
  572. if (c == 4 && tx == NULL &&
  573. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  574. omap2_mcspi_set_enable(spi, 0);
  575. *rx++ = __raw_readl(rx_reg);
  576. dev_vdbg(&spi->dev, "read-%d %08x\n",
  577. word_len, *(rx - 1));
  578. if (mcspi_wait_for_reg_bit(chstat_reg,
  579. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  580. dev_err(&spi->dev,
  581. "RXS timed out\n");
  582. goto out;
  583. }
  584. c = 0;
  585. } else if (c == 0 && tx == NULL) {
  586. omap2_mcspi_set_enable(spi, 0);
  587. }
  588. *rx++ = __raw_readl(rx_reg);
  589. dev_vdbg(&spi->dev, "read-%d %08x\n",
  590. word_len, *(rx - 1));
  591. }
  592. } while (c >= 4);
  593. }
  594. /* for TX_ONLY mode, be sure all words have shifted out */
  595. if (xfer->rx_buf == NULL) {
  596. if (mcspi_wait_for_reg_bit(chstat_reg,
  597. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  598. dev_err(&spi->dev, "TXS timed out\n");
  599. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  600. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  601. dev_err(&spi->dev, "EOT timed out\n");
  602. /* disable chan to purge rx datas received in TX_ONLY transfer,
  603. * otherwise these rx datas will affect the direct following
  604. * RX_ONLY transfer.
  605. */
  606. omap2_mcspi_set_enable(spi, 0);
  607. }
  608. out:
  609. omap2_mcspi_set_enable(spi, 1);
  610. return count - c;
  611. }
  612. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  613. {
  614. u32 div;
  615. for (div = 0; div < 15; div++)
  616. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  617. return div;
  618. return 15;
  619. }
  620. /* called only when no transfer is active to this device */
  621. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  622. struct spi_transfer *t)
  623. {
  624. struct omap2_mcspi_cs *cs = spi->controller_state;
  625. struct omap2_mcspi *mcspi;
  626. struct spi_master *spi_cntrl;
  627. u32 l = 0, div = 0;
  628. u8 word_len = spi->bits_per_word;
  629. u32 speed_hz = spi->max_speed_hz;
  630. mcspi = spi_master_get_devdata(spi->master);
  631. spi_cntrl = mcspi->master;
  632. if (t != NULL && t->bits_per_word)
  633. word_len = t->bits_per_word;
  634. cs->word_len = word_len;
  635. if (t && t->speed_hz)
  636. speed_hz = t->speed_hz;
  637. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  638. div = omap2_mcspi_calc_divisor(speed_hz);
  639. l = mcspi_cached_chconf0(spi);
  640. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  641. * REVISIT: this controller could support SPI_3WIRE mode.
  642. */
  643. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  644. l &= ~OMAP2_MCSPI_CHCONF_IS;
  645. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  646. l |= OMAP2_MCSPI_CHCONF_DPE0;
  647. } else {
  648. l |= OMAP2_MCSPI_CHCONF_IS;
  649. l |= OMAP2_MCSPI_CHCONF_DPE1;
  650. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  651. }
  652. /* wordlength */
  653. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  654. l |= (word_len - 1) << 7;
  655. /* set chipselect polarity; manage with FORCE */
  656. if (!(spi->mode & SPI_CS_HIGH))
  657. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  658. else
  659. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  660. /* set clock divisor */
  661. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  662. l |= div << 2;
  663. /* set SPI mode 0..3 */
  664. if (spi->mode & SPI_CPOL)
  665. l |= OMAP2_MCSPI_CHCONF_POL;
  666. else
  667. l &= ~OMAP2_MCSPI_CHCONF_POL;
  668. if (spi->mode & SPI_CPHA)
  669. l |= OMAP2_MCSPI_CHCONF_PHA;
  670. else
  671. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  672. mcspi_write_chconf0(spi, l);
  673. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  674. OMAP2_MCSPI_MAX_FREQ >> div,
  675. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  676. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  677. return 0;
  678. }
  679. static int omap2_mcspi_request_dma(struct spi_device *spi)
  680. {
  681. struct spi_master *master = spi->master;
  682. struct omap2_mcspi *mcspi;
  683. struct omap2_mcspi_dma *mcspi_dma;
  684. dma_cap_mask_t mask;
  685. unsigned sig;
  686. mcspi = spi_master_get_devdata(master);
  687. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  688. init_completion(&mcspi_dma->dma_rx_completion);
  689. init_completion(&mcspi_dma->dma_tx_completion);
  690. dma_cap_zero(mask);
  691. dma_cap_set(DMA_SLAVE, mask);
  692. sig = mcspi_dma->dma_rx_sync_dev;
  693. mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  694. if (!mcspi_dma->dma_rx) {
  695. dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
  696. return -EAGAIN;
  697. }
  698. sig = mcspi_dma->dma_tx_sync_dev;
  699. mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  700. if (!mcspi_dma->dma_tx) {
  701. dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
  702. dma_release_channel(mcspi_dma->dma_rx);
  703. mcspi_dma->dma_rx = NULL;
  704. return -EAGAIN;
  705. }
  706. return 0;
  707. }
  708. static int omap2_mcspi_setup(struct spi_device *spi)
  709. {
  710. int ret;
  711. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  712. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  713. struct omap2_mcspi_dma *mcspi_dma;
  714. struct omap2_mcspi_cs *cs = spi->controller_state;
  715. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  716. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  717. spi->bits_per_word);
  718. return -EINVAL;
  719. }
  720. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  721. if (!cs) {
  722. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  723. if (!cs)
  724. return -ENOMEM;
  725. cs->base = mcspi->base + spi->chip_select * 0x14;
  726. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  727. cs->chconf0 = 0;
  728. spi->controller_state = cs;
  729. /* Link this to context save list */
  730. list_add_tail(&cs->node, &ctx->cs);
  731. }
  732. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  733. ret = omap2_mcspi_request_dma(spi);
  734. if (ret < 0)
  735. return ret;
  736. }
  737. ret = pm_runtime_get_sync(mcspi->dev);
  738. if (ret < 0)
  739. return ret;
  740. ret = omap2_mcspi_setup_transfer(spi, NULL);
  741. pm_runtime_mark_last_busy(mcspi->dev);
  742. pm_runtime_put_autosuspend(mcspi->dev);
  743. return ret;
  744. }
  745. static void omap2_mcspi_cleanup(struct spi_device *spi)
  746. {
  747. struct omap2_mcspi *mcspi;
  748. struct omap2_mcspi_dma *mcspi_dma;
  749. struct omap2_mcspi_cs *cs;
  750. mcspi = spi_master_get_devdata(spi->master);
  751. if (spi->controller_state) {
  752. /* Unlink controller state from context save list */
  753. cs = spi->controller_state;
  754. list_del(&cs->node);
  755. kfree(cs);
  756. }
  757. if (spi->chip_select < spi->master->num_chipselect) {
  758. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  759. if (mcspi_dma->dma_rx) {
  760. dma_release_channel(mcspi_dma->dma_rx);
  761. mcspi_dma->dma_rx = NULL;
  762. }
  763. if (mcspi_dma->dma_tx) {
  764. dma_release_channel(mcspi_dma->dma_tx);
  765. mcspi_dma->dma_tx = NULL;
  766. }
  767. }
  768. }
  769. static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
  770. {
  771. /* We only enable one channel at a time -- the one whose message is
  772. * -- although this controller would gladly
  773. * arbitrate among multiple channels. This corresponds to "single
  774. * channel" master mode. As a side effect, we need to manage the
  775. * chipselect with the FORCE bit ... CS != channel enable.
  776. */
  777. struct spi_device *spi;
  778. struct spi_transfer *t = NULL;
  779. int cs_active = 0;
  780. struct omap2_mcspi_cs *cs;
  781. struct omap2_mcspi_device_config *cd;
  782. int par_override = 0;
  783. int status = 0;
  784. u32 chconf;
  785. spi = m->spi;
  786. cs = spi->controller_state;
  787. cd = spi->controller_data;
  788. omap2_mcspi_set_enable(spi, 1);
  789. list_for_each_entry(t, &m->transfers, transfer_list) {
  790. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  791. status = -EINVAL;
  792. break;
  793. }
  794. if (par_override || t->speed_hz || t->bits_per_word) {
  795. par_override = 1;
  796. status = omap2_mcspi_setup_transfer(spi, t);
  797. if (status < 0)
  798. break;
  799. if (!t->speed_hz && !t->bits_per_word)
  800. par_override = 0;
  801. }
  802. if (!cs_active) {
  803. omap2_mcspi_force_cs(spi, 1);
  804. cs_active = 1;
  805. }
  806. chconf = mcspi_cached_chconf0(spi);
  807. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  808. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  809. if (t->tx_buf == NULL)
  810. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  811. else if (t->rx_buf == NULL)
  812. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  813. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  814. /* Turbo mode is for more than one word */
  815. if (t->len > ((cs->word_len + 7) >> 3))
  816. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  817. }
  818. mcspi_write_chconf0(spi, chconf);
  819. if (t->len) {
  820. unsigned count;
  821. /* RX_ONLY mode needs dummy data in TX reg */
  822. if (t->tx_buf == NULL)
  823. __raw_writel(0, cs->base
  824. + OMAP2_MCSPI_TX0);
  825. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  826. count = omap2_mcspi_txrx_dma(spi, t);
  827. else
  828. count = omap2_mcspi_txrx_pio(spi, t);
  829. m->actual_length += count;
  830. if (count != t->len) {
  831. status = -EIO;
  832. break;
  833. }
  834. }
  835. if (t->delay_usecs)
  836. udelay(t->delay_usecs);
  837. /* ignore the "leave it on after last xfer" hint */
  838. if (t->cs_change) {
  839. omap2_mcspi_force_cs(spi, 0);
  840. cs_active = 0;
  841. }
  842. }
  843. /* Restore defaults if they were overriden */
  844. if (par_override) {
  845. par_override = 0;
  846. status = omap2_mcspi_setup_transfer(spi, NULL);
  847. }
  848. if (cs_active)
  849. omap2_mcspi_force_cs(spi, 0);
  850. omap2_mcspi_set_enable(spi, 0);
  851. m->status = status;
  852. }
  853. static int omap2_mcspi_transfer_one_message(struct spi_master *master,
  854. struct spi_message *m)
  855. {
  856. struct omap2_mcspi *mcspi;
  857. struct spi_transfer *t;
  858. mcspi = spi_master_get_devdata(master);
  859. m->actual_length = 0;
  860. m->status = 0;
  861. /* reject invalid messages and transfers */
  862. if (list_empty(&m->transfers))
  863. return -EINVAL;
  864. list_for_each_entry(t, &m->transfers, transfer_list) {
  865. const void *tx_buf = t->tx_buf;
  866. void *rx_buf = t->rx_buf;
  867. unsigned len = t->len;
  868. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  869. || (len && !(rx_buf || tx_buf))
  870. || (t->bits_per_word &&
  871. ( t->bits_per_word < 4
  872. || t->bits_per_word > 32))) {
  873. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  874. t->speed_hz,
  875. len,
  876. tx_buf ? "tx" : "",
  877. rx_buf ? "rx" : "",
  878. t->bits_per_word);
  879. return -EINVAL;
  880. }
  881. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  882. dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
  883. t->speed_hz,
  884. OMAP2_MCSPI_MAX_FREQ >> 15);
  885. return -EINVAL;
  886. }
  887. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  888. continue;
  889. if (tx_buf != NULL) {
  890. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  891. len, DMA_TO_DEVICE);
  892. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  893. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  894. 'T', len);
  895. return -EINVAL;
  896. }
  897. }
  898. if (rx_buf != NULL) {
  899. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  900. DMA_FROM_DEVICE);
  901. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  902. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  903. 'R', len);
  904. if (tx_buf != NULL)
  905. dma_unmap_single(mcspi->dev, t->tx_dma,
  906. len, DMA_TO_DEVICE);
  907. return -EINVAL;
  908. }
  909. }
  910. }
  911. omap2_mcspi_work(mcspi, m);
  912. spi_finalize_current_message(master);
  913. return 0;
  914. }
  915. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  916. {
  917. struct spi_master *master = mcspi->master;
  918. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  919. int ret = 0;
  920. ret = pm_runtime_get_sync(mcspi->dev);
  921. if (ret < 0)
  922. return ret;
  923. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  924. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  925. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  926. omap2_mcspi_set_master_mode(master);
  927. pm_runtime_mark_last_busy(mcspi->dev);
  928. pm_runtime_put_autosuspend(mcspi->dev);
  929. return 0;
  930. }
  931. static int omap_mcspi_runtime_resume(struct device *dev)
  932. {
  933. struct omap2_mcspi *mcspi;
  934. struct spi_master *master;
  935. master = dev_get_drvdata(dev);
  936. mcspi = spi_master_get_devdata(master);
  937. omap2_mcspi_restore_ctx(mcspi);
  938. return 0;
  939. }
  940. static struct omap2_mcspi_platform_config omap2_pdata = {
  941. .regs_offset = 0,
  942. };
  943. static struct omap2_mcspi_platform_config omap4_pdata = {
  944. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  945. };
  946. static const struct of_device_id omap_mcspi_of_match[] = {
  947. {
  948. .compatible = "ti,omap2-mcspi",
  949. .data = &omap2_pdata,
  950. },
  951. {
  952. .compatible = "ti,omap4-mcspi",
  953. .data = &omap4_pdata,
  954. },
  955. { },
  956. };
  957. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  958. static int omap2_mcspi_probe(struct platform_device *pdev)
  959. {
  960. struct spi_master *master;
  961. const struct omap2_mcspi_platform_config *pdata;
  962. struct omap2_mcspi *mcspi;
  963. struct resource *r;
  964. int status = 0, i;
  965. u32 regs_offset = 0;
  966. static int bus_num = 1;
  967. struct device_node *node = pdev->dev.of_node;
  968. const struct of_device_id *match;
  969. struct pinctrl *pinctrl;
  970. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  971. if (master == NULL) {
  972. dev_dbg(&pdev->dev, "master allocation failed\n");
  973. return -ENOMEM;
  974. }
  975. /* the spi->mode bits understood by this driver: */
  976. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  977. master->setup = omap2_mcspi_setup;
  978. master->prepare_transfer_hardware = omap2_prepare_transfer;
  979. master->unprepare_transfer_hardware = omap2_unprepare_transfer;
  980. master->transfer_one_message = omap2_mcspi_transfer_one_message;
  981. master->cleanup = omap2_mcspi_cleanup;
  982. master->dev.of_node = node;
  983. dev_set_drvdata(&pdev->dev, master);
  984. mcspi = spi_master_get_devdata(master);
  985. mcspi->master = master;
  986. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  987. if (match) {
  988. u32 num_cs = 1; /* default number of chipselect */
  989. pdata = match->data;
  990. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  991. master->num_chipselect = num_cs;
  992. master->bus_num = bus_num++;
  993. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  994. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  995. } else {
  996. pdata = pdev->dev.platform_data;
  997. master->num_chipselect = pdata->num_cs;
  998. if (pdev->id != -1)
  999. master->bus_num = pdev->id;
  1000. mcspi->pin_dir = pdata->pin_dir;
  1001. }
  1002. regs_offset = pdata->regs_offset;
  1003. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1004. if (r == NULL) {
  1005. status = -ENODEV;
  1006. goto free_master;
  1007. }
  1008. r->start += regs_offset;
  1009. r->end += regs_offset;
  1010. mcspi->phys = r->start;
  1011. mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
  1012. if (!mcspi->base) {
  1013. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  1014. status = -ENOMEM;
  1015. goto free_master;
  1016. }
  1017. mcspi->dev = &pdev->dev;
  1018. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1019. mcspi->dma_channels = kcalloc(master->num_chipselect,
  1020. sizeof(struct omap2_mcspi_dma),
  1021. GFP_KERNEL);
  1022. if (mcspi->dma_channels == NULL)
  1023. goto free_master;
  1024. for (i = 0; i < master->num_chipselect; i++) {
  1025. char dma_ch_name[14];
  1026. struct resource *dma_res;
  1027. sprintf(dma_ch_name, "rx%d", i);
  1028. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1029. dma_ch_name);
  1030. if (!dma_res) {
  1031. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  1032. status = -ENODEV;
  1033. break;
  1034. }
  1035. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  1036. sprintf(dma_ch_name, "tx%d", i);
  1037. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1038. dma_ch_name);
  1039. if (!dma_res) {
  1040. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  1041. status = -ENODEV;
  1042. break;
  1043. }
  1044. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  1045. }
  1046. if (status < 0)
  1047. goto dma_chnl_free;
  1048. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1049. if (IS_ERR(pinctrl))
  1050. dev_warn(&pdev->dev,
  1051. "pins are not configured from the driver\n");
  1052. pm_runtime_use_autosuspend(&pdev->dev);
  1053. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1054. pm_runtime_enable(&pdev->dev);
  1055. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  1056. goto disable_pm;
  1057. status = spi_register_master(master);
  1058. if (status < 0)
  1059. goto disable_pm;
  1060. return status;
  1061. disable_pm:
  1062. pm_runtime_disable(&pdev->dev);
  1063. dma_chnl_free:
  1064. kfree(mcspi->dma_channels);
  1065. free_master:
  1066. spi_master_put(master);
  1067. return status;
  1068. }
  1069. static int omap2_mcspi_remove(struct platform_device *pdev)
  1070. {
  1071. struct spi_master *master;
  1072. struct omap2_mcspi *mcspi;
  1073. struct omap2_mcspi_dma *dma_channels;
  1074. master = dev_get_drvdata(&pdev->dev);
  1075. mcspi = spi_master_get_devdata(master);
  1076. dma_channels = mcspi->dma_channels;
  1077. pm_runtime_put_sync(mcspi->dev);
  1078. pm_runtime_disable(&pdev->dev);
  1079. spi_unregister_master(master);
  1080. kfree(dma_channels);
  1081. return 0;
  1082. }
  1083. /* work with hotplug and coldplug */
  1084. MODULE_ALIAS("platform:omap2_mcspi");
  1085. #ifdef CONFIG_SUSPEND
  1086. /*
  1087. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1088. * unactive state when driver was suspend, then force it to unactive state at
  1089. * wake up.
  1090. */
  1091. static int omap2_mcspi_resume(struct device *dev)
  1092. {
  1093. struct spi_master *master = dev_get_drvdata(dev);
  1094. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1095. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1096. struct omap2_mcspi_cs *cs;
  1097. pm_runtime_get_sync(mcspi->dev);
  1098. list_for_each_entry(cs, &ctx->cs, node) {
  1099. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1100. /*
  1101. * We need to toggle CS state for OMAP take this
  1102. * change in account.
  1103. */
  1104. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1105. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1106. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1107. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1108. }
  1109. }
  1110. pm_runtime_mark_last_busy(mcspi->dev);
  1111. pm_runtime_put_autosuspend(mcspi->dev);
  1112. return 0;
  1113. }
  1114. #else
  1115. #define omap2_mcspi_resume NULL
  1116. #endif
  1117. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1118. .resume = omap2_mcspi_resume,
  1119. .runtime_resume = omap_mcspi_runtime_resume,
  1120. };
  1121. static struct platform_driver omap2_mcspi_driver = {
  1122. .driver = {
  1123. .name = "omap2_mcspi",
  1124. .owner = THIS_MODULE,
  1125. .pm = &omap2_mcspi_pm_ops,
  1126. .of_match_table = omap_mcspi_of_match,
  1127. },
  1128. .probe = omap2_mcspi_probe,
  1129. .remove = omap2_mcspi_remove,
  1130. };
  1131. module_platform_driver(omap2_mcspi_driver);
  1132. MODULE_LICENSE("GPL");