spi-omap2-mcspi.c 37 KB

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