spi-omap2-mcspi.c 34 KB

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