spi-omap2-mcspi.c 33 KB

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