spi-omap2-mcspi.c 37 KB

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