spi-tegra20-slink.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kthread.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/spi-tegra.h>
  37. #include <mach/clk.h>
  38. #define SLINK_COMMAND 0x000
  39. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  40. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  41. #define SLINK_BOTH_EN (1 << 10)
  42. #define SLINK_CS_SW (1 << 11)
  43. #define SLINK_CS_VALUE (1 << 12)
  44. #define SLINK_CS_POLARITY (1 << 13)
  45. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  46. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  47. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  48. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  49. #define SLINK_IDLE_SDA_MASK (3 << 16)
  50. #define SLINK_CS_POLARITY1 (1 << 20)
  51. #define SLINK_CK_SDA (1 << 21)
  52. #define SLINK_CS_POLARITY2 (1 << 22)
  53. #define SLINK_CS_POLARITY3 (1 << 23)
  54. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  55. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  56. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  57. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  58. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  59. #define SLINK_M_S (1 << 28)
  60. #define SLINK_WAIT (1 << 29)
  61. #define SLINK_GO (1 << 30)
  62. #define SLINK_ENB (1 << 31)
  63. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  64. #define SLINK_COMMAND2 0x004
  65. #define SLINK_LSBFE (1 << 0)
  66. #define SLINK_SSOE (1 << 1)
  67. #define SLINK_SPIE (1 << 4)
  68. #define SLINK_BIDIROE (1 << 6)
  69. #define SLINK_MODFEN (1 << 7)
  70. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  71. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  72. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  73. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  74. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  75. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  76. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  77. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  78. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  79. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  80. #define SLINK_SPC0 (1 << 29)
  81. #define SLINK_TXEN (1 << 30)
  82. #define SLINK_RXEN (1 << 31)
  83. #define SLINK_STATUS 0x008
  84. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  85. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  86. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  87. #define SLINK_MODF (1 << 16)
  88. #define SLINK_RX_UNF (1 << 18)
  89. #define SLINK_TX_OVF (1 << 19)
  90. #define SLINK_TX_FULL (1 << 20)
  91. #define SLINK_TX_EMPTY (1 << 21)
  92. #define SLINK_RX_FULL (1 << 22)
  93. #define SLINK_RX_EMPTY (1 << 23)
  94. #define SLINK_TX_UNF (1 << 24)
  95. #define SLINK_RX_OVF (1 << 25)
  96. #define SLINK_TX_FLUSH (1 << 26)
  97. #define SLINK_RX_FLUSH (1 << 27)
  98. #define SLINK_SCLK (1 << 28)
  99. #define SLINK_ERR (1 << 29)
  100. #define SLINK_RDY (1 << 30)
  101. #define SLINK_BSY (1 << 31)
  102. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  103. SLINK_TX_UNF | SLINK_RX_OVF)
  104. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  105. #define SLINK_MAS_DATA 0x010
  106. #define SLINK_SLAVE_DATA 0x014
  107. #define SLINK_DMA_CTL 0x018
  108. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  109. #define SLINK_TX_TRIG_1 (0 << 16)
  110. #define SLINK_TX_TRIG_4 (1 << 16)
  111. #define SLINK_TX_TRIG_8 (2 << 16)
  112. #define SLINK_TX_TRIG_16 (3 << 16)
  113. #define SLINK_TX_TRIG_MASK (3 << 16)
  114. #define SLINK_RX_TRIG_1 (0 << 18)
  115. #define SLINK_RX_TRIG_4 (1 << 18)
  116. #define SLINK_RX_TRIG_8 (2 << 18)
  117. #define SLINK_RX_TRIG_16 (3 << 18)
  118. #define SLINK_RX_TRIG_MASK (3 << 18)
  119. #define SLINK_PACKED (1 << 20)
  120. #define SLINK_PACK_SIZE_4 (0 << 21)
  121. #define SLINK_PACK_SIZE_8 (1 << 21)
  122. #define SLINK_PACK_SIZE_16 (2 << 21)
  123. #define SLINK_PACK_SIZE_32 (3 << 21)
  124. #define SLINK_PACK_SIZE_MASK (3 << 21)
  125. #define SLINK_IE_TXC (1 << 26)
  126. #define SLINK_IE_RXC (1 << 27)
  127. #define SLINK_DMA_EN (1 << 31)
  128. #define SLINK_STATUS2 0x01c
  129. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  130. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  131. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  132. #define SLINK_TX_FIFO 0x100
  133. #define SLINK_RX_FIFO 0x180
  134. #define DATA_DIR_TX (1 << 0)
  135. #define DATA_DIR_RX (1 << 1)
  136. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  137. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  138. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  139. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  140. #define SLINK_STATUS2_RESET \
  141. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  142. #define MAX_CHIP_SELECT 4
  143. #define SLINK_FIFO_DEPTH 32
  144. struct tegra_slink_chip_data {
  145. bool cs_hold_time;
  146. };
  147. struct tegra_slink_data {
  148. struct device *dev;
  149. struct spi_master *master;
  150. const struct tegra_slink_chip_data *chip_data;
  151. spinlock_t lock;
  152. struct clk *clk;
  153. void __iomem *base;
  154. phys_addr_t phys;
  155. unsigned irq;
  156. int dma_req_sel;
  157. u32 spi_max_frequency;
  158. u32 cur_speed;
  159. struct spi_device *cur_spi;
  160. unsigned cur_pos;
  161. unsigned cur_len;
  162. unsigned words_per_32bit;
  163. unsigned bytes_per_word;
  164. unsigned curr_dma_words;
  165. unsigned cur_direction;
  166. unsigned cur_rx_pos;
  167. unsigned cur_tx_pos;
  168. unsigned dma_buf_size;
  169. unsigned max_buf_size;
  170. bool is_curr_dma_xfer;
  171. bool is_hw_based_cs;
  172. struct completion rx_dma_complete;
  173. struct completion tx_dma_complete;
  174. u32 tx_status;
  175. u32 rx_status;
  176. u32 status_reg;
  177. bool is_packed;
  178. unsigned long packed_size;
  179. u32 command_reg;
  180. u32 command2_reg;
  181. u32 dma_control_reg;
  182. u32 def_command_reg;
  183. u32 def_command2_reg;
  184. struct completion xfer_completion;
  185. struct spi_transfer *curr_xfer;
  186. struct dma_chan *rx_dma_chan;
  187. u32 *rx_dma_buf;
  188. dma_addr_t rx_dma_phys;
  189. struct dma_async_tx_descriptor *rx_dma_desc;
  190. struct dma_chan *tx_dma_chan;
  191. u32 *tx_dma_buf;
  192. dma_addr_t tx_dma_phys;
  193. struct dma_async_tx_descriptor *tx_dma_desc;
  194. };
  195. static int tegra_slink_runtime_suspend(struct device *dev);
  196. static int tegra_slink_runtime_resume(struct device *dev);
  197. static inline unsigned long tegra_slink_readl(struct tegra_slink_data *tspi,
  198. unsigned long reg)
  199. {
  200. return readl(tspi->base + reg);
  201. }
  202. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  203. unsigned long val, unsigned long reg)
  204. {
  205. writel(val, tspi->base + reg);
  206. /* Read back register to make sure that register writes completed */
  207. if (reg != SLINK_TX_FIFO)
  208. readl(tspi->base + SLINK_MAS_DATA);
  209. }
  210. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  211. {
  212. unsigned long val;
  213. unsigned long val_write = 0;
  214. val = tegra_slink_readl(tspi, SLINK_STATUS);
  215. /* Write 1 to clear status register */
  216. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  217. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  218. }
  219. static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  220. struct spi_transfer *t)
  221. {
  222. unsigned long val;
  223. switch (tspi->bytes_per_word) {
  224. case 0:
  225. val = SLINK_PACK_SIZE_4;
  226. break;
  227. case 1:
  228. val = SLINK_PACK_SIZE_8;
  229. break;
  230. case 2:
  231. val = SLINK_PACK_SIZE_16;
  232. break;
  233. case 4:
  234. val = SLINK_PACK_SIZE_32;
  235. break;
  236. default:
  237. val = 0;
  238. }
  239. return val;
  240. }
  241. static unsigned tegra_slink_calculate_curr_xfer_param(
  242. struct spi_device *spi, struct tegra_slink_data *tspi,
  243. struct spi_transfer *t)
  244. {
  245. unsigned remain_len = t->len - tspi->cur_pos;
  246. unsigned max_word;
  247. unsigned bits_per_word ;
  248. unsigned max_len;
  249. unsigned total_fifo_words;
  250. bits_per_word = t->bits_per_word ? t->bits_per_word :
  251. spi->bits_per_word;
  252. tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;
  253. if (bits_per_word == 8 || bits_per_word == 16) {
  254. tspi->is_packed = 1;
  255. tspi->words_per_32bit = 32/bits_per_word;
  256. } else {
  257. tspi->is_packed = 0;
  258. tspi->words_per_32bit = 1;
  259. }
  260. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  261. if (tspi->is_packed) {
  262. max_len = min(remain_len, tspi->max_buf_size);
  263. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  264. total_fifo_words = max_len/4;
  265. } else {
  266. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  267. max_word = min(max_word, tspi->max_buf_size/4);
  268. tspi->curr_dma_words = max_word;
  269. total_fifo_words = max_word;
  270. }
  271. return total_fifo_words;
  272. }
  273. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  274. struct tegra_slink_data *tspi, struct spi_transfer *t)
  275. {
  276. unsigned nbytes;
  277. unsigned tx_empty_count;
  278. unsigned long fifo_status;
  279. unsigned max_n_32bit;
  280. unsigned i, count;
  281. unsigned long x;
  282. unsigned int written_words;
  283. unsigned fifo_words_left;
  284. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  285. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  286. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  287. if (tspi->is_packed) {
  288. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  289. written_words = min(fifo_words_left, tspi->curr_dma_words);
  290. nbytes = written_words * tspi->bytes_per_word;
  291. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  292. for (count = 0; count < max_n_32bit; count++) {
  293. x = 0;
  294. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  295. x |= (*tx_buf++) << (i*8);
  296. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  297. }
  298. } else {
  299. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  300. written_words = max_n_32bit;
  301. nbytes = written_words * tspi->bytes_per_word;
  302. for (count = 0; count < max_n_32bit; count++) {
  303. x = 0;
  304. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  305. i++, nbytes--)
  306. x |= ((*tx_buf++) << i*8);
  307. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  308. }
  309. }
  310. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  311. return written_words;
  312. }
  313. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  314. struct tegra_slink_data *tspi, struct spi_transfer *t)
  315. {
  316. unsigned rx_full_count;
  317. unsigned long fifo_status;
  318. unsigned i, count;
  319. unsigned long x;
  320. unsigned int read_words = 0;
  321. unsigned len;
  322. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  323. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  324. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  325. if (tspi->is_packed) {
  326. len = tspi->curr_dma_words * tspi->bytes_per_word;
  327. for (count = 0; count < rx_full_count; count++) {
  328. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  329. for (i = 0; len && (i < 4); i++, len--)
  330. *rx_buf++ = (x >> i*8) & 0xFF;
  331. }
  332. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  333. read_words += tspi->curr_dma_words;
  334. } else {
  335. unsigned int bits_per_word;
  336. bits_per_word = t->bits_per_word ? t->bits_per_word :
  337. tspi->cur_spi->bits_per_word;
  338. for (count = 0; count < rx_full_count; count++) {
  339. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  340. for (i = 0; (i < tspi->bytes_per_word); i++)
  341. *rx_buf++ = (x >> (i*8)) & 0xFF;
  342. }
  343. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  344. read_words += rx_full_count;
  345. }
  346. return read_words;
  347. }
  348. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  349. struct tegra_slink_data *tspi, struct spi_transfer *t)
  350. {
  351. unsigned len;
  352. /* Make the dma buffer to read by cpu */
  353. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  354. tspi->dma_buf_size, DMA_TO_DEVICE);
  355. if (tspi->is_packed) {
  356. len = tspi->curr_dma_words * tspi->bytes_per_word;
  357. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  358. } else {
  359. unsigned int i;
  360. unsigned int count;
  361. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  362. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  363. unsigned int x;
  364. for (count = 0; count < tspi->curr_dma_words; count++) {
  365. x = 0;
  366. for (i = 0; consume && (i < tspi->bytes_per_word);
  367. i++, consume--)
  368. x |= ((*tx_buf++) << i * 8);
  369. tspi->tx_dma_buf[count] = x;
  370. }
  371. }
  372. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  373. /* Make the dma buffer to read by dma */
  374. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  375. tspi->dma_buf_size, DMA_TO_DEVICE);
  376. }
  377. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  378. struct tegra_slink_data *tspi, struct spi_transfer *t)
  379. {
  380. unsigned len;
  381. /* Make the dma buffer to read by cpu */
  382. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  383. tspi->dma_buf_size, DMA_FROM_DEVICE);
  384. if (tspi->is_packed) {
  385. len = tspi->curr_dma_words * tspi->bytes_per_word;
  386. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  387. } else {
  388. unsigned int i;
  389. unsigned int count;
  390. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  391. unsigned int x;
  392. unsigned int rx_mask, bits_per_word;
  393. bits_per_word = t->bits_per_word ? t->bits_per_word :
  394. tspi->cur_spi->bits_per_word;
  395. rx_mask = (1 << bits_per_word) - 1;
  396. for (count = 0; count < tspi->curr_dma_words; count++) {
  397. x = tspi->rx_dma_buf[count];
  398. x &= rx_mask;
  399. for (i = 0; (i < tspi->bytes_per_word); i++)
  400. *rx_buf++ = (x >> (i*8)) & 0xFF;
  401. }
  402. }
  403. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  404. /* Make the dma buffer to read by dma */
  405. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  406. tspi->dma_buf_size, DMA_FROM_DEVICE);
  407. }
  408. static void tegra_slink_dma_complete(void *args)
  409. {
  410. struct completion *dma_complete = args;
  411. complete(dma_complete);
  412. }
  413. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  414. {
  415. INIT_COMPLETION(tspi->tx_dma_complete);
  416. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  417. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  418. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  419. if (!tspi->tx_dma_desc) {
  420. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  421. return -EIO;
  422. }
  423. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  424. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  425. dmaengine_submit(tspi->tx_dma_desc);
  426. dma_async_issue_pending(tspi->tx_dma_chan);
  427. return 0;
  428. }
  429. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  430. {
  431. INIT_COMPLETION(tspi->rx_dma_complete);
  432. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  433. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  434. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  435. if (!tspi->rx_dma_desc) {
  436. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  437. return -EIO;
  438. }
  439. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  440. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  441. dmaengine_submit(tspi->rx_dma_desc);
  442. dma_async_issue_pending(tspi->rx_dma_chan);
  443. return 0;
  444. }
  445. static int tegra_slink_start_dma_based_transfer(
  446. struct tegra_slink_data *tspi, struct spi_transfer *t)
  447. {
  448. unsigned long val;
  449. unsigned long test_val;
  450. unsigned int len;
  451. int ret = 0;
  452. unsigned long status;
  453. /* Make sure that Rx and Tx fifo are empty */
  454. status = tegra_slink_readl(tspi, SLINK_STATUS);
  455. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  456. dev_err(tspi->dev,
  457. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  458. return -EIO;
  459. }
  460. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  461. val |= tspi->packed_size;
  462. if (tspi->is_packed)
  463. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  464. 4) * 4;
  465. else
  466. len = tspi->curr_dma_words * 4;
  467. /* Set attention level based on length of transfer */
  468. if (len & 0xF)
  469. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  470. else if (((len) >> 4) & 0x1)
  471. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  472. else
  473. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  474. if (tspi->cur_direction & DATA_DIR_TX)
  475. val |= SLINK_IE_TXC;
  476. if (tspi->cur_direction & DATA_DIR_RX)
  477. val |= SLINK_IE_RXC;
  478. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  479. tspi->dma_control_reg = val;
  480. if (tspi->cur_direction & DATA_DIR_TX) {
  481. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  482. wmb();
  483. ret = tegra_slink_start_tx_dma(tspi, len);
  484. if (ret < 0) {
  485. dev_err(tspi->dev,
  486. "Starting tx dma failed, err %d\n", ret);
  487. return ret;
  488. }
  489. /* Wait for tx fifo to be fill before starting slink */
  490. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  491. while (!(test_val & SLINK_TX_FULL))
  492. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  493. }
  494. if (tspi->cur_direction & DATA_DIR_RX) {
  495. /* Make the dma buffer to read by dma */
  496. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  497. tspi->dma_buf_size, DMA_FROM_DEVICE);
  498. ret = tegra_slink_start_rx_dma(tspi, len);
  499. if (ret < 0) {
  500. dev_err(tspi->dev,
  501. "Starting rx dma failed, err %d\n", ret);
  502. if (tspi->cur_direction & DATA_DIR_TX)
  503. dmaengine_terminate_all(tspi->tx_dma_chan);
  504. return ret;
  505. }
  506. }
  507. tspi->is_curr_dma_xfer = true;
  508. if (tspi->is_packed) {
  509. val |= SLINK_PACKED;
  510. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  511. /* HW need small delay after settign Packed mode */
  512. udelay(1);
  513. }
  514. tspi->dma_control_reg = val;
  515. val |= SLINK_DMA_EN;
  516. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  517. return ret;
  518. }
  519. static int tegra_slink_start_cpu_based_transfer(
  520. struct tegra_slink_data *tspi, struct spi_transfer *t)
  521. {
  522. unsigned long val;
  523. unsigned cur_words;
  524. val = tspi->packed_size;
  525. if (tspi->cur_direction & DATA_DIR_TX)
  526. val |= SLINK_IE_TXC;
  527. if (tspi->cur_direction & DATA_DIR_RX)
  528. val |= SLINK_IE_RXC;
  529. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  530. tspi->dma_control_reg = val;
  531. if (tspi->cur_direction & DATA_DIR_TX)
  532. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  533. else
  534. cur_words = tspi->curr_dma_words;
  535. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  536. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  537. tspi->dma_control_reg = val;
  538. tspi->is_curr_dma_xfer = false;
  539. if (tspi->is_packed) {
  540. val |= SLINK_PACKED;
  541. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  542. udelay(1);
  543. wmb();
  544. }
  545. tspi->dma_control_reg = val;
  546. val |= SLINK_DMA_EN;
  547. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  548. return 0;
  549. }
  550. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  551. bool dma_to_memory)
  552. {
  553. struct dma_chan *dma_chan;
  554. u32 *dma_buf;
  555. dma_addr_t dma_phys;
  556. int ret;
  557. struct dma_slave_config dma_sconfig;
  558. dma_cap_mask_t mask;
  559. dma_cap_zero(mask);
  560. dma_cap_set(DMA_SLAVE, mask);
  561. dma_chan = dma_request_channel(mask, NULL, NULL);
  562. if (!dma_chan) {
  563. dev_err(tspi->dev,
  564. "Dma channel is not available, will try later\n");
  565. return -EPROBE_DEFER;
  566. }
  567. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  568. &dma_phys, GFP_KERNEL);
  569. if (!dma_buf) {
  570. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  571. dma_release_channel(dma_chan);
  572. return -ENOMEM;
  573. }
  574. dma_sconfig.slave_id = tspi->dma_req_sel;
  575. if (dma_to_memory) {
  576. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  577. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  578. dma_sconfig.src_maxburst = 0;
  579. } else {
  580. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  581. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  582. dma_sconfig.dst_maxburst = 0;
  583. }
  584. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  585. if (ret)
  586. goto scrub;
  587. if (dma_to_memory) {
  588. tspi->rx_dma_chan = dma_chan;
  589. tspi->rx_dma_buf = dma_buf;
  590. tspi->rx_dma_phys = dma_phys;
  591. } else {
  592. tspi->tx_dma_chan = dma_chan;
  593. tspi->tx_dma_buf = dma_buf;
  594. tspi->tx_dma_phys = dma_phys;
  595. }
  596. return 0;
  597. scrub:
  598. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  599. dma_release_channel(dma_chan);
  600. return ret;
  601. }
  602. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  603. bool dma_to_memory)
  604. {
  605. u32 *dma_buf;
  606. dma_addr_t dma_phys;
  607. struct dma_chan *dma_chan;
  608. if (dma_to_memory) {
  609. dma_buf = tspi->rx_dma_buf;
  610. dma_chan = tspi->rx_dma_chan;
  611. dma_phys = tspi->rx_dma_phys;
  612. tspi->rx_dma_chan = NULL;
  613. tspi->rx_dma_buf = NULL;
  614. } else {
  615. dma_buf = tspi->tx_dma_buf;
  616. dma_chan = tspi->tx_dma_chan;
  617. dma_phys = tspi->tx_dma_phys;
  618. tspi->tx_dma_buf = NULL;
  619. tspi->tx_dma_chan = NULL;
  620. }
  621. if (!dma_chan)
  622. return;
  623. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  624. dma_release_channel(dma_chan);
  625. }
  626. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  627. struct spi_transfer *t, bool is_first_of_msg,
  628. bool is_single_xfer)
  629. {
  630. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  631. u32 speed;
  632. u8 bits_per_word;
  633. unsigned total_fifo_words;
  634. int ret;
  635. struct tegra_spi_device_controller_data *cdata = spi->controller_data;
  636. unsigned long command;
  637. unsigned long command2;
  638. bits_per_word = t->bits_per_word;
  639. speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  640. if (!speed)
  641. speed = tspi->spi_max_frequency;
  642. if (speed != tspi->cur_speed) {
  643. clk_set_rate(tspi->clk, speed * 4);
  644. tspi->cur_speed = speed;
  645. }
  646. tspi->cur_spi = spi;
  647. tspi->cur_pos = 0;
  648. tspi->cur_rx_pos = 0;
  649. tspi->cur_tx_pos = 0;
  650. tspi->curr_xfer = t;
  651. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  652. if (is_first_of_msg) {
  653. tegra_slink_clear_status(tspi);
  654. command = tspi->def_command_reg;
  655. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  656. command2 = tspi->def_command2_reg;
  657. command2 |= SLINK_SS_EN_CS(spi->chip_select);
  658. /* possibly use the hw based chip select */
  659. tspi->is_hw_based_cs = false;
  660. if (cdata && cdata->is_hw_based_cs && is_single_xfer &&
  661. ((tspi->curr_dma_words * tspi->bytes_per_word) ==
  662. (t->len - tspi->cur_pos))) {
  663. int setup_count;
  664. int sts2;
  665. setup_count = cdata->cs_setup_clk_count >> 1;
  666. setup_count = max(setup_count, 3);
  667. command2 |= SLINK_SS_SETUP(setup_count);
  668. if (tspi->chip_data->cs_hold_time) {
  669. int hold_count;
  670. hold_count = cdata->cs_hold_clk_count;
  671. hold_count = max(hold_count, 0xF);
  672. sts2 = tegra_slink_readl(tspi, SLINK_STATUS2);
  673. sts2 &= ~SLINK_SS_HOLD_TIME(0xF);
  674. sts2 |= SLINK_SS_HOLD_TIME(hold_count);
  675. tegra_slink_writel(tspi, sts2, SLINK_STATUS2);
  676. }
  677. tspi->is_hw_based_cs = true;
  678. }
  679. if (tspi->is_hw_based_cs)
  680. command &= ~SLINK_CS_SW;
  681. else
  682. command |= SLINK_CS_SW | SLINK_CS_VALUE;
  683. command &= ~SLINK_MODES;
  684. if (spi->mode & SPI_CPHA)
  685. command |= SLINK_CK_SDA;
  686. if (spi->mode & SPI_CPOL)
  687. command |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  688. else
  689. command |= SLINK_IDLE_SCLK_DRIVE_LOW;
  690. } else {
  691. command = tspi->command_reg;
  692. command &= ~SLINK_BIT_LENGTH(~0);
  693. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  694. command2 = tspi->command2_reg;
  695. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  696. }
  697. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  698. tspi->command_reg = command;
  699. tspi->cur_direction = 0;
  700. if (t->rx_buf) {
  701. command2 |= SLINK_RXEN;
  702. tspi->cur_direction |= DATA_DIR_RX;
  703. }
  704. if (t->tx_buf) {
  705. command2 |= SLINK_TXEN;
  706. tspi->cur_direction |= DATA_DIR_TX;
  707. }
  708. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  709. tspi->command2_reg = command2;
  710. if (total_fifo_words > SLINK_FIFO_DEPTH)
  711. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  712. else
  713. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  714. return ret;
  715. }
  716. static int tegra_slink_setup(struct spi_device *spi)
  717. {
  718. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  719. unsigned long val;
  720. unsigned long flags;
  721. int ret;
  722. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  723. SLINK_CS_POLARITY,
  724. SLINK_CS_POLARITY1,
  725. SLINK_CS_POLARITY2,
  726. SLINK_CS_POLARITY3,
  727. };
  728. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  729. spi->bits_per_word,
  730. spi->mode & SPI_CPOL ? "" : "~",
  731. spi->mode & SPI_CPHA ? "" : "~",
  732. spi->max_speed_hz);
  733. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  734. ret = pm_runtime_get_sync(tspi->dev);
  735. if (ret < 0) {
  736. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  737. return ret;
  738. }
  739. spin_lock_irqsave(&tspi->lock, flags);
  740. val = tspi->def_command_reg;
  741. if (spi->mode & SPI_CS_HIGH)
  742. val |= cs_pol_bit[spi->chip_select];
  743. else
  744. val &= ~cs_pol_bit[spi->chip_select];
  745. tspi->def_command_reg = val;
  746. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  747. spin_unlock_irqrestore(&tspi->lock, flags);
  748. pm_runtime_put(tspi->dev);
  749. return 0;
  750. }
  751. static int tegra_slink_prepare_transfer(struct spi_master *master)
  752. {
  753. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  754. return pm_runtime_get_sync(tspi->dev);
  755. }
  756. static int tegra_slink_unprepare_transfer(struct spi_master *master)
  757. {
  758. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  759. pm_runtime_put(tspi->dev);
  760. return 0;
  761. }
  762. static int tegra_slink_transfer_one_message(struct spi_master *master,
  763. struct spi_message *msg)
  764. {
  765. bool is_first_msg = true;
  766. int single_xfer;
  767. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  768. struct spi_transfer *xfer;
  769. struct spi_device *spi = msg->spi;
  770. int ret;
  771. msg->status = 0;
  772. msg->actual_length = 0;
  773. single_xfer = list_is_singular(&msg->transfers);
  774. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  775. INIT_COMPLETION(tspi->xfer_completion);
  776. ret = tegra_slink_start_transfer_one(spi, xfer,
  777. is_first_msg, single_xfer);
  778. if (ret < 0) {
  779. dev_err(tspi->dev,
  780. "spi can not start transfer, err %d\n", ret);
  781. goto exit;
  782. }
  783. is_first_msg = false;
  784. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  785. SLINK_DMA_TIMEOUT);
  786. if (WARN_ON(ret == 0)) {
  787. dev_err(tspi->dev,
  788. "spi trasfer timeout, err %d\n", ret);
  789. ret = -EIO;
  790. goto exit;
  791. }
  792. if (tspi->tx_status || tspi->rx_status) {
  793. dev_err(tspi->dev, "Error in Transfer\n");
  794. ret = -EIO;
  795. goto exit;
  796. }
  797. msg->actual_length += xfer->len;
  798. if (xfer->cs_change && xfer->delay_usecs) {
  799. tegra_slink_writel(tspi, tspi->def_command_reg,
  800. SLINK_COMMAND);
  801. udelay(xfer->delay_usecs);
  802. }
  803. }
  804. ret = 0;
  805. exit:
  806. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  807. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  808. msg->status = ret;
  809. spi_finalize_current_message(master);
  810. return ret;
  811. }
  812. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  813. {
  814. struct spi_transfer *t = tspi->curr_xfer;
  815. unsigned long flags;
  816. spin_lock_irqsave(&tspi->lock, flags);
  817. if (tspi->tx_status || tspi->rx_status ||
  818. (tspi->status_reg & SLINK_BSY)) {
  819. dev_err(tspi->dev,
  820. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  821. dev_err(tspi->dev,
  822. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  823. tspi->command2_reg, tspi->dma_control_reg);
  824. tegra_periph_reset_assert(tspi->clk);
  825. udelay(2);
  826. tegra_periph_reset_deassert(tspi->clk);
  827. complete(&tspi->xfer_completion);
  828. goto exit;
  829. }
  830. if (tspi->cur_direction & DATA_DIR_RX)
  831. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  832. if (tspi->cur_direction & DATA_DIR_TX)
  833. tspi->cur_pos = tspi->cur_tx_pos;
  834. else
  835. tspi->cur_pos = tspi->cur_rx_pos;
  836. if (tspi->cur_pos == t->len) {
  837. complete(&tspi->xfer_completion);
  838. goto exit;
  839. }
  840. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  841. tegra_slink_start_cpu_based_transfer(tspi, t);
  842. exit:
  843. spin_unlock_irqrestore(&tspi->lock, flags);
  844. return IRQ_HANDLED;
  845. }
  846. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  847. {
  848. struct spi_transfer *t = tspi->curr_xfer;
  849. long wait_status;
  850. int err = 0;
  851. unsigned total_fifo_words;
  852. unsigned long flags;
  853. /* Abort dmas if any error */
  854. if (tspi->cur_direction & DATA_DIR_TX) {
  855. if (tspi->tx_status) {
  856. dmaengine_terminate_all(tspi->tx_dma_chan);
  857. err += 1;
  858. } else {
  859. wait_status = wait_for_completion_interruptible_timeout(
  860. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  861. if (wait_status <= 0) {
  862. dmaengine_terminate_all(tspi->tx_dma_chan);
  863. dev_err(tspi->dev, "TxDma Xfer failed\n");
  864. err += 1;
  865. }
  866. }
  867. }
  868. if (tspi->cur_direction & DATA_DIR_RX) {
  869. if (tspi->rx_status) {
  870. dmaengine_terminate_all(tspi->rx_dma_chan);
  871. err += 2;
  872. } else {
  873. wait_status = wait_for_completion_interruptible_timeout(
  874. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  875. if (wait_status <= 0) {
  876. dmaengine_terminate_all(tspi->rx_dma_chan);
  877. dev_err(tspi->dev, "RxDma Xfer failed\n");
  878. err += 2;
  879. }
  880. }
  881. }
  882. spin_lock_irqsave(&tspi->lock, flags);
  883. if (err) {
  884. dev_err(tspi->dev,
  885. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  886. dev_err(tspi->dev,
  887. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  888. tspi->command2_reg, tspi->dma_control_reg);
  889. tegra_periph_reset_assert(tspi->clk);
  890. udelay(2);
  891. tegra_periph_reset_deassert(tspi->clk);
  892. complete(&tspi->xfer_completion);
  893. spin_unlock_irqrestore(&tspi->lock, flags);
  894. return IRQ_HANDLED;
  895. }
  896. if (tspi->cur_direction & DATA_DIR_RX)
  897. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  898. if (tspi->cur_direction & DATA_DIR_TX)
  899. tspi->cur_pos = tspi->cur_tx_pos;
  900. else
  901. tspi->cur_pos = tspi->cur_rx_pos;
  902. if (tspi->cur_pos == t->len) {
  903. complete(&tspi->xfer_completion);
  904. goto exit;
  905. }
  906. /* Continue transfer in current message */
  907. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  908. tspi, t);
  909. if (total_fifo_words > SLINK_FIFO_DEPTH)
  910. err = tegra_slink_start_dma_based_transfer(tspi, t);
  911. else
  912. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  913. exit:
  914. spin_unlock_irqrestore(&tspi->lock, flags);
  915. return IRQ_HANDLED;
  916. }
  917. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  918. {
  919. struct tegra_slink_data *tspi = context_data;
  920. if (!tspi->is_curr_dma_xfer)
  921. return handle_cpu_based_xfer(tspi);
  922. return handle_dma_based_xfer(tspi);
  923. }
  924. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  925. {
  926. struct tegra_slink_data *tspi = context_data;
  927. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  928. if (tspi->cur_direction & DATA_DIR_TX)
  929. tspi->tx_status = tspi->status_reg &
  930. (SLINK_TX_OVF | SLINK_TX_UNF);
  931. if (tspi->cur_direction & DATA_DIR_RX)
  932. tspi->rx_status = tspi->status_reg &
  933. (SLINK_RX_OVF | SLINK_RX_UNF);
  934. tegra_slink_clear_status(tspi);
  935. return IRQ_WAKE_THREAD;
  936. }
  937. static struct tegra_spi_platform_data *tegra_slink_parse_dt(
  938. struct platform_device *pdev)
  939. {
  940. struct tegra_spi_platform_data *pdata;
  941. const unsigned int *prop;
  942. struct device_node *np = pdev->dev.of_node;
  943. u32 of_dma[2];
  944. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  945. if (!pdata) {
  946. dev_err(&pdev->dev, "Memory alloc for pdata failed\n");
  947. return NULL;
  948. }
  949. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  950. of_dma, 2) >= 0)
  951. pdata->dma_req_sel = of_dma[1];
  952. prop = of_get_property(np, "spi-max-frequency", NULL);
  953. if (prop)
  954. pdata->spi_max_frequency = be32_to_cpup(prop);
  955. return pdata;
  956. }
  957. const struct tegra_slink_chip_data tegra30_spi_cdata = {
  958. .cs_hold_time = true,
  959. };
  960. const struct tegra_slink_chip_data tegra20_spi_cdata = {
  961. .cs_hold_time = false,
  962. };
  963. static struct of_device_id tegra_slink_of_match[] = {
  964. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  965. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  966. {}
  967. };
  968. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  969. static int tegra_slink_probe(struct platform_device *pdev)
  970. {
  971. struct spi_master *master;
  972. struct tegra_slink_data *tspi;
  973. struct resource *r;
  974. struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;
  975. int ret, spi_irq;
  976. const struct tegra_slink_chip_data *cdata = NULL;
  977. const struct of_device_id *match;
  978. match = of_match_device(of_match_ptr(tegra_slink_of_match), &pdev->dev);
  979. if (!match) {
  980. dev_err(&pdev->dev, "Error: No device match found\n");
  981. return -ENODEV;
  982. }
  983. cdata = match->data;
  984. if (!pdata && pdev->dev.of_node)
  985. pdata = tegra_slink_parse_dt(pdev);
  986. if (!pdata) {
  987. dev_err(&pdev->dev, "No platform data, exiting\n");
  988. return -ENODEV;
  989. }
  990. if (!pdata->spi_max_frequency)
  991. pdata->spi_max_frequency = 25000000; /* 25MHz */
  992. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  993. if (!master) {
  994. dev_err(&pdev->dev, "master allocation failed\n");
  995. return -ENOMEM;
  996. }
  997. /* the spi->mode bits understood by this driver: */
  998. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  999. master->setup = tegra_slink_setup;
  1000. master->prepare_transfer_hardware = tegra_slink_prepare_transfer;
  1001. master->transfer_one_message = tegra_slink_transfer_one_message;
  1002. master->unprepare_transfer_hardware = tegra_slink_unprepare_transfer;
  1003. master->num_chipselect = MAX_CHIP_SELECT;
  1004. master->bus_num = -1;
  1005. dev_set_drvdata(&pdev->dev, master);
  1006. tspi = spi_master_get_devdata(master);
  1007. tspi->master = master;
  1008. tspi->dma_req_sel = pdata->dma_req_sel;
  1009. tspi->dev = &pdev->dev;
  1010. tspi->chip_data = cdata;
  1011. spin_lock_init(&tspi->lock);
  1012. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1013. if (!r) {
  1014. dev_err(&pdev->dev, "No IO memory resource\n");
  1015. ret = -ENODEV;
  1016. goto exit_free_master;
  1017. }
  1018. tspi->phys = r->start;
  1019. tspi->base = devm_request_and_ioremap(&pdev->dev, r);
  1020. if (!tspi->base) {
  1021. dev_err(&pdev->dev,
  1022. "Cannot request memregion/iomap dma address\n");
  1023. ret = -EADDRNOTAVAIL;
  1024. goto exit_free_master;
  1025. }
  1026. spi_irq = platform_get_irq(pdev, 0);
  1027. tspi->irq = spi_irq;
  1028. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  1029. tegra_slink_isr_thread, IRQF_ONESHOT,
  1030. dev_name(&pdev->dev), tspi);
  1031. if (ret < 0) {
  1032. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  1033. tspi->irq);
  1034. goto exit_free_master;
  1035. }
  1036. tspi->clk = devm_clk_get(&pdev->dev, "slink");
  1037. if (IS_ERR(tspi->clk)) {
  1038. dev_err(&pdev->dev, "can not get clock\n");
  1039. ret = PTR_ERR(tspi->clk);
  1040. goto exit_free_irq;
  1041. }
  1042. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  1043. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  1044. tspi->spi_max_frequency = pdata->spi_max_frequency;
  1045. if (pdata->dma_req_sel) {
  1046. ret = tegra_slink_init_dma_param(tspi, true);
  1047. if (ret < 0) {
  1048. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  1049. goto exit_free_irq;
  1050. }
  1051. ret = tegra_slink_init_dma_param(tspi, false);
  1052. if (ret < 0) {
  1053. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  1054. goto exit_rx_dma_free;
  1055. }
  1056. tspi->max_buf_size = tspi->dma_buf_size;
  1057. init_completion(&tspi->tx_dma_complete);
  1058. init_completion(&tspi->rx_dma_complete);
  1059. }
  1060. init_completion(&tspi->xfer_completion);
  1061. pm_runtime_enable(&pdev->dev);
  1062. if (!pm_runtime_enabled(&pdev->dev)) {
  1063. ret = tegra_slink_runtime_resume(&pdev->dev);
  1064. if (ret)
  1065. goto exit_pm_disable;
  1066. }
  1067. ret = pm_runtime_get_sync(&pdev->dev);
  1068. if (ret < 0) {
  1069. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1070. goto exit_pm_disable;
  1071. }
  1072. tspi->def_command_reg = SLINK_M_S;
  1073. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  1074. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  1075. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  1076. pm_runtime_put(&pdev->dev);
  1077. master->dev.of_node = pdev->dev.of_node;
  1078. ret = spi_register_master(master);
  1079. if (ret < 0) {
  1080. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1081. goto exit_pm_disable;
  1082. }
  1083. return ret;
  1084. exit_pm_disable:
  1085. pm_runtime_disable(&pdev->dev);
  1086. if (!pm_runtime_status_suspended(&pdev->dev))
  1087. tegra_slink_runtime_suspend(&pdev->dev);
  1088. tegra_slink_deinit_dma_param(tspi, false);
  1089. exit_rx_dma_free:
  1090. tegra_slink_deinit_dma_param(tspi, true);
  1091. exit_free_irq:
  1092. free_irq(spi_irq, tspi);
  1093. exit_free_master:
  1094. spi_master_put(master);
  1095. return ret;
  1096. }
  1097. static int tegra_slink_remove(struct platform_device *pdev)
  1098. {
  1099. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  1100. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1101. free_irq(tspi->irq, tspi);
  1102. spi_unregister_master(master);
  1103. if (tspi->tx_dma_chan)
  1104. tegra_slink_deinit_dma_param(tspi, false);
  1105. if (tspi->rx_dma_chan)
  1106. tegra_slink_deinit_dma_param(tspi, true);
  1107. pm_runtime_disable(&pdev->dev);
  1108. if (!pm_runtime_status_suspended(&pdev->dev))
  1109. tegra_slink_runtime_suspend(&pdev->dev);
  1110. return 0;
  1111. }
  1112. #ifdef CONFIG_PM_SLEEP
  1113. static int tegra_slink_suspend(struct device *dev)
  1114. {
  1115. struct spi_master *master = dev_get_drvdata(dev);
  1116. return spi_master_suspend(master);
  1117. }
  1118. static int tegra_slink_resume(struct device *dev)
  1119. {
  1120. struct spi_master *master = dev_get_drvdata(dev);
  1121. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1122. int ret;
  1123. ret = pm_runtime_get_sync(dev);
  1124. if (ret < 0) {
  1125. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1126. return ret;
  1127. }
  1128. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1129. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1130. pm_runtime_put(dev);
  1131. return spi_master_resume(master);
  1132. }
  1133. #endif
  1134. static int tegra_slink_runtime_suspend(struct device *dev)
  1135. {
  1136. struct spi_master *master = dev_get_drvdata(dev);
  1137. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1138. /* Flush all write which are in PPSB queue by reading back */
  1139. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1140. clk_disable_unprepare(tspi->clk);
  1141. return 0;
  1142. }
  1143. static int tegra_slink_runtime_resume(struct device *dev)
  1144. {
  1145. struct spi_master *master = dev_get_drvdata(dev);
  1146. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1147. int ret;
  1148. ret = clk_prepare_enable(tspi->clk);
  1149. if (ret < 0) {
  1150. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1151. return ret;
  1152. }
  1153. return 0;
  1154. }
  1155. static const struct dev_pm_ops slink_pm_ops = {
  1156. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1157. tegra_slink_runtime_resume, NULL)
  1158. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1159. };
  1160. static struct platform_driver tegra_slink_driver = {
  1161. .driver = {
  1162. .name = "spi-tegra-slink",
  1163. .owner = THIS_MODULE,
  1164. .pm = &slink_pm_ops,
  1165. .of_match_table = of_match_ptr(tegra_slink_of_match),
  1166. },
  1167. .probe = tegra_slink_probe,
  1168. .remove = tegra_slink_remove,
  1169. };
  1170. module_platform_driver(tegra_slink_driver);
  1171. MODULE_ALIAS("platform:spi-tegra-slink");
  1172. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1173. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1174. MODULE_LICENSE("GPL v2");