spi-topcliff-pch.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. /*
  2. * SPI bus driver for the Topcliff PCH used by Intel SoCs
  3. *
  4. * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/pci.h>
  17. #include <linux/wait.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/sched.h>
  21. #include <linux/spi/spidev.h>
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/dmaengine.h>
  26. #include <linux/pch_dma.h>
  27. /* Register offsets */
  28. #define PCH_SPCR 0x00 /* SPI control register */
  29. #define PCH_SPBRR 0x04 /* SPI baud rate register */
  30. #define PCH_SPSR 0x08 /* SPI status register */
  31. #define PCH_SPDWR 0x0C /* SPI write data register */
  32. #define PCH_SPDRR 0x10 /* SPI read data register */
  33. #define PCH_SSNXCR 0x18 /* SSN Expand Control Register */
  34. #define PCH_SRST 0x1C /* SPI reset register */
  35. #define PCH_ADDRESS_SIZE 0x20
  36. #define PCH_SPSR_TFD 0x000007C0
  37. #define PCH_SPSR_RFD 0x0000F800
  38. #define PCH_READABLE(x) (((x) & PCH_SPSR_RFD)>>11)
  39. #define PCH_WRITABLE(x) (((x) & PCH_SPSR_TFD)>>6)
  40. #define PCH_RX_THOLD 7
  41. #define PCH_RX_THOLD_MAX 15
  42. #define PCH_TX_THOLD 2
  43. #define PCH_MAX_BAUDRATE 5000000
  44. #define PCH_MAX_FIFO_DEPTH 16
  45. #define STATUS_RUNNING 1
  46. #define STATUS_EXITING 2
  47. #define PCH_SLEEP_TIME 10
  48. #define SSN_LOW 0x02U
  49. #define SSN_HIGH 0x03U
  50. #define SSN_NO_CONTROL 0x00U
  51. #define PCH_MAX_CS 0xFF
  52. #define PCI_DEVICE_ID_GE_SPI 0x8816
  53. #define SPCR_SPE_BIT (1 << 0)
  54. #define SPCR_MSTR_BIT (1 << 1)
  55. #define SPCR_LSBF_BIT (1 << 4)
  56. #define SPCR_CPHA_BIT (1 << 5)
  57. #define SPCR_CPOL_BIT (1 << 6)
  58. #define SPCR_TFIE_BIT (1 << 8)
  59. #define SPCR_RFIE_BIT (1 << 9)
  60. #define SPCR_FIE_BIT (1 << 10)
  61. #define SPCR_ORIE_BIT (1 << 11)
  62. #define SPCR_MDFIE_BIT (1 << 12)
  63. #define SPCR_FICLR_BIT (1 << 24)
  64. #define SPSR_TFI_BIT (1 << 0)
  65. #define SPSR_RFI_BIT (1 << 1)
  66. #define SPSR_FI_BIT (1 << 2)
  67. #define SPSR_ORF_BIT (1 << 3)
  68. #define SPBRR_SIZE_BIT (1 << 10)
  69. #define PCH_ALL (SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|\
  70. SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
  71. #define SPCR_RFIC_FIELD 20
  72. #define SPCR_TFIC_FIELD 16
  73. #define MASK_SPBRR_SPBR_BITS ((1 << 10) - 1)
  74. #define MASK_RFIC_SPCR_BITS (0xf << SPCR_RFIC_FIELD)
  75. #define MASK_TFIC_SPCR_BITS (0xf << SPCR_TFIC_FIELD)
  76. #define PCH_CLOCK_HZ 50000000
  77. #define PCH_MAX_SPBR 1023
  78. /* Definition for ML7213/ML7223/ML7831 by LAPIS Semiconductor */
  79. #define PCI_VENDOR_ID_ROHM 0x10DB
  80. #define PCI_DEVICE_ID_ML7213_SPI 0x802c
  81. #define PCI_DEVICE_ID_ML7223_SPI 0x800F
  82. #define PCI_DEVICE_ID_ML7831_SPI 0x8816
  83. /*
  84. * Set the number of SPI instance max
  85. * Intel EG20T PCH : 1ch
  86. * LAPIS Semiconductor ML7213 IOH : 2ch
  87. * LAPIS Semiconductor ML7223 IOH : 1ch
  88. * LAPIS Semiconductor ML7831 IOH : 1ch
  89. */
  90. #define PCH_SPI_MAX_DEV 2
  91. #define PCH_BUF_SIZE 4096
  92. #define PCH_DMA_TRANS_SIZE 12
  93. static int use_dma = 1;
  94. struct pch_spi_dma_ctrl {
  95. struct dma_async_tx_descriptor *desc_tx;
  96. struct dma_async_tx_descriptor *desc_rx;
  97. struct pch_dma_slave param_tx;
  98. struct pch_dma_slave param_rx;
  99. struct dma_chan *chan_tx;
  100. struct dma_chan *chan_rx;
  101. struct scatterlist *sg_tx_p;
  102. struct scatterlist *sg_rx_p;
  103. struct scatterlist sg_tx;
  104. struct scatterlist sg_rx;
  105. int nent;
  106. void *tx_buf_virt;
  107. void *rx_buf_virt;
  108. dma_addr_t tx_buf_dma;
  109. dma_addr_t rx_buf_dma;
  110. };
  111. /**
  112. * struct pch_spi_data - Holds the SPI channel specific details
  113. * @io_remap_addr: The remapped PCI base address
  114. * @master: Pointer to the SPI master structure
  115. * @work: Reference to work queue handler
  116. * @wk: Workqueue for carrying out execution of the
  117. * requests
  118. * @wait: Wait queue for waking up upon receiving an
  119. * interrupt.
  120. * @transfer_complete: Status of SPI Transfer
  121. * @bcurrent_msg_processing: Status flag for message processing
  122. * @lock: Lock for protecting this structure
  123. * @queue: SPI Message queue
  124. * @status: Status of the SPI driver
  125. * @bpw_len: Length of data to be transferred in bits per
  126. * word
  127. * @transfer_active: Flag showing active transfer
  128. * @tx_index: Transmit data count; for bookkeeping during
  129. * transfer
  130. * @rx_index: Receive data count; for bookkeeping during
  131. * transfer
  132. * @tx_buff: Buffer for data to be transmitted
  133. * @rx_index: Buffer for Received data
  134. * @n_curnt_chip: The chip number that this SPI driver currently
  135. * operates on
  136. * @current_chip: Reference to the current chip that this SPI
  137. * driver currently operates on
  138. * @current_msg: The current message that this SPI driver is
  139. * handling
  140. * @cur_trans: The current transfer that this SPI driver is
  141. * handling
  142. * @board_dat: Reference to the SPI device data structure
  143. * @plat_dev: platform_device structure
  144. * @ch: SPI channel number
  145. * @irq_reg_sts: Status of IRQ registration
  146. */
  147. struct pch_spi_data {
  148. void __iomem *io_remap_addr;
  149. unsigned long io_base_addr;
  150. struct spi_master *master;
  151. struct work_struct work;
  152. struct workqueue_struct *wk;
  153. wait_queue_head_t wait;
  154. u8 transfer_complete;
  155. u8 bcurrent_msg_processing;
  156. spinlock_t lock;
  157. struct list_head queue;
  158. u8 status;
  159. u32 bpw_len;
  160. u8 transfer_active;
  161. u32 tx_index;
  162. u32 rx_index;
  163. u16 *pkt_tx_buff;
  164. u16 *pkt_rx_buff;
  165. u8 n_curnt_chip;
  166. struct spi_device *current_chip;
  167. struct spi_message *current_msg;
  168. struct spi_transfer *cur_trans;
  169. struct pch_spi_board_data *board_dat;
  170. struct platform_device *plat_dev;
  171. int ch;
  172. struct pch_spi_dma_ctrl dma;
  173. int use_dma;
  174. u8 irq_reg_sts;
  175. int save_total_len;
  176. };
  177. /**
  178. * struct pch_spi_board_data - Holds the SPI device specific details
  179. * @pdev: Pointer to the PCI device
  180. * @suspend_sts: Status of suspend
  181. * @num: The number of SPI device instance
  182. */
  183. struct pch_spi_board_data {
  184. struct pci_dev *pdev;
  185. u8 suspend_sts;
  186. int num;
  187. };
  188. struct pch_pd_dev_save {
  189. int num;
  190. struct platform_device *pd_save[PCH_SPI_MAX_DEV];
  191. struct pch_spi_board_data *board_dat;
  192. };
  193. static const struct pci_device_id pch_spi_pcidev_id[] = {
  194. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_GE_SPI), 1, },
  195. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_SPI), 2, },
  196. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_SPI), 1, },
  197. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_SPI), 1, },
  198. { }
  199. };
  200. /**
  201. * pch_spi_writereg() - Performs register writes
  202. * @master: Pointer to struct spi_master.
  203. * @idx: Register offset.
  204. * @val: Value to be written to register.
  205. */
  206. static inline void pch_spi_writereg(struct spi_master *master, int idx, u32 val)
  207. {
  208. struct pch_spi_data *data = spi_master_get_devdata(master);
  209. iowrite32(val, (data->io_remap_addr + idx));
  210. }
  211. /**
  212. * pch_spi_readreg() - Performs register reads
  213. * @master: Pointer to struct spi_master.
  214. * @idx: Register offset.
  215. */
  216. static inline u32 pch_spi_readreg(struct spi_master *master, int idx)
  217. {
  218. struct pch_spi_data *data = spi_master_get_devdata(master);
  219. return ioread32(data->io_remap_addr + idx);
  220. }
  221. static inline void pch_spi_setclr_reg(struct spi_master *master, int idx,
  222. u32 set, u32 clr)
  223. {
  224. u32 tmp = pch_spi_readreg(master, idx);
  225. tmp = (tmp & ~clr) | set;
  226. pch_spi_writereg(master, idx, tmp);
  227. }
  228. static void pch_spi_set_master_mode(struct spi_master *master)
  229. {
  230. pch_spi_setclr_reg(master, PCH_SPCR, SPCR_MSTR_BIT, 0);
  231. }
  232. /**
  233. * pch_spi_clear_fifo() - Clears the Transmit and Receive FIFOs
  234. * @master: Pointer to struct spi_master.
  235. */
  236. static void pch_spi_clear_fifo(struct spi_master *master)
  237. {
  238. pch_spi_setclr_reg(master, PCH_SPCR, SPCR_FICLR_BIT, 0);
  239. pch_spi_setclr_reg(master, PCH_SPCR, 0, SPCR_FICLR_BIT);
  240. }
  241. static void pch_spi_handler_sub(struct pch_spi_data *data, u32 reg_spsr_val,
  242. void __iomem *io_remap_addr)
  243. {
  244. u32 n_read, tx_index, rx_index, bpw_len;
  245. u16 *pkt_rx_buffer, *pkt_tx_buff;
  246. int read_cnt;
  247. u32 reg_spcr_val;
  248. void __iomem *spsr;
  249. void __iomem *spdrr;
  250. void __iomem *spdwr;
  251. spsr = io_remap_addr + PCH_SPSR;
  252. iowrite32(reg_spsr_val, spsr);
  253. if (data->transfer_active) {
  254. rx_index = data->rx_index;
  255. tx_index = data->tx_index;
  256. bpw_len = data->bpw_len;
  257. pkt_rx_buffer = data->pkt_rx_buff;
  258. pkt_tx_buff = data->pkt_tx_buff;
  259. spdrr = io_remap_addr + PCH_SPDRR;
  260. spdwr = io_remap_addr + PCH_SPDWR;
  261. n_read = PCH_READABLE(reg_spsr_val);
  262. for (read_cnt = 0; (read_cnt < n_read); read_cnt++) {
  263. pkt_rx_buffer[rx_index++] = ioread32(spdrr);
  264. if (tx_index < bpw_len)
  265. iowrite32(pkt_tx_buff[tx_index++], spdwr);
  266. }
  267. /* disable RFI if not needed */
  268. if ((bpw_len - rx_index) <= PCH_MAX_FIFO_DEPTH) {
  269. reg_spcr_val = ioread32(io_remap_addr + PCH_SPCR);
  270. reg_spcr_val &= ~SPCR_RFIE_BIT; /* disable RFI */
  271. /* reset rx threshold */
  272. reg_spcr_val &= ~MASK_RFIC_SPCR_BITS;
  273. reg_spcr_val |= (PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD);
  274. iowrite32(reg_spcr_val, (io_remap_addr + PCH_SPCR));
  275. }
  276. /* update counts */
  277. data->tx_index = tx_index;
  278. data->rx_index = rx_index;
  279. /* if transfer complete interrupt */
  280. if (reg_spsr_val & SPSR_FI_BIT) {
  281. if ((tx_index == bpw_len) && (rx_index == tx_index)) {
  282. /* disable interrupts */
  283. pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
  284. PCH_ALL);
  285. /* transfer is completed;
  286. inform pch_spi_process_messages */
  287. data->transfer_complete = true;
  288. data->transfer_active = false;
  289. wake_up(&data->wait);
  290. } else {
  291. dev_vdbg(&data->master->dev,
  292. "%s : Transfer is not completed",
  293. __func__);
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * pch_spi_handler() - Interrupt handler
  300. * @irq: The interrupt number.
  301. * @dev_id: Pointer to struct pch_spi_board_data.
  302. */
  303. static irqreturn_t pch_spi_handler(int irq, void *dev_id)
  304. {
  305. u32 reg_spsr_val;
  306. void __iomem *spsr;
  307. void __iomem *io_remap_addr;
  308. irqreturn_t ret = IRQ_NONE;
  309. struct pch_spi_data *data = dev_id;
  310. struct pch_spi_board_data *board_dat = data->board_dat;
  311. if (board_dat->suspend_sts) {
  312. dev_dbg(&board_dat->pdev->dev,
  313. "%s returning due to suspend\n", __func__);
  314. return IRQ_NONE;
  315. }
  316. io_remap_addr = data->io_remap_addr;
  317. spsr = io_remap_addr + PCH_SPSR;
  318. reg_spsr_val = ioread32(spsr);
  319. if (reg_spsr_val & SPSR_ORF_BIT) {
  320. dev_err(&board_dat->pdev->dev, "%s Over run error\n", __func__);
  321. if (data->current_msg->complete) {
  322. data->transfer_complete = true;
  323. data->current_msg->status = -EIO;
  324. data->current_msg->complete(data->current_msg->context);
  325. data->bcurrent_msg_processing = false;
  326. data->current_msg = NULL;
  327. data->cur_trans = NULL;
  328. }
  329. }
  330. if (data->use_dma)
  331. return IRQ_NONE;
  332. /* Check if the interrupt is for SPI device */
  333. if (reg_spsr_val & (SPSR_FI_BIT | SPSR_RFI_BIT)) {
  334. pch_spi_handler_sub(data, reg_spsr_val, io_remap_addr);
  335. ret = IRQ_HANDLED;
  336. }
  337. dev_dbg(&board_dat->pdev->dev, "%s EXIT return value=%d\n",
  338. __func__, ret);
  339. return ret;
  340. }
  341. /**
  342. * pch_spi_set_baud_rate() - Sets SPBR field in SPBRR
  343. * @master: Pointer to struct spi_master.
  344. * @speed_hz: Baud rate.
  345. */
  346. static void pch_spi_set_baud_rate(struct spi_master *master, u32 speed_hz)
  347. {
  348. u32 n_spbr = PCH_CLOCK_HZ / (speed_hz * 2);
  349. /* if baud rate is less than we can support limit it */
  350. if (n_spbr > PCH_MAX_SPBR)
  351. n_spbr = PCH_MAX_SPBR;
  352. pch_spi_setclr_reg(master, PCH_SPBRR, n_spbr, MASK_SPBRR_SPBR_BITS);
  353. }
  354. /**
  355. * pch_spi_set_bits_per_word() - Sets SIZE field in SPBRR
  356. * @master: Pointer to struct spi_master.
  357. * @bits_per_word: Bits per word for SPI transfer.
  358. */
  359. static void pch_spi_set_bits_per_word(struct spi_master *master,
  360. u8 bits_per_word)
  361. {
  362. if (bits_per_word == 8)
  363. pch_spi_setclr_reg(master, PCH_SPBRR, 0, SPBRR_SIZE_BIT);
  364. else
  365. pch_spi_setclr_reg(master, PCH_SPBRR, SPBRR_SIZE_BIT, 0);
  366. }
  367. /**
  368. * pch_spi_setup_transfer() - Configures the PCH SPI hardware for transfer
  369. * @spi: Pointer to struct spi_device.
  370. */
  371. static void pch_spi_setup_transfer(struct spi_device *spi)
  372. {
  373. u32 flags = 0;
  374. dev_dbg(&spi->dev, "%s SPBRR content =%x setting baud rate=%d\n",
  375. __func__, pch_spi_readreg(spi->master, PCH_SPBRR),
  376. spi->max_speed_hz);
  377. pch_spi_set_baud_rate(spi->master, spi->max_speed_hz);
  378. /* set bits per word */
  379. pch_spi_set_bits_per_word(spi->master, spi->bits_per_word);
  380. if (!(spi->mode & SPI_LSB_FIRST))
  381. flags |= SPCR_LSBF_BIT;
  382. if (spi->mode & SPI_CPOL)
  383. flags |= SPCR_CPOL_BIT;
  384. if (spi->mode & SPI_CPHA)
  385. flags |= SPCR_CPHA_BIT;
  386. pch_spi_setclr_reg(spi->master, PCH_SPCR, flags,
  387. (SPCR_LSBF_BIT | SPCR_CPOL_BIT | SPCR_CPHA_BIT));
  388. /* Clear the FIFO by toggling FICLR to 1 and back to 0 */
  389. pch_spi_clear_fifo(spi->master);
  390. }
  391. /**
  392. * pch_spi_reset() - Clears SPI registers
  393. * @master: Pointer to struct spi_master.
  394. */
  395. static void pch_spi_reset(struct spi_master *master)
  396. {
  397. /* write 1 to reset SPI */
  398. pch_spi_writereg(master, PCH_SRST, 0x1);
  399. /* clear reset */
  400. pch_spi_writereg(master, PCH_SRST, 0x0);
  401. }
  402. static int pch_spi_transfer(struct spi_device *pspi, struct spi_message *pmsg)
  403. {
  404. struct spi_transfer *transfer;
  405. struct pch_spi_data *data = spi_master_get_devdata(pspi->master);
  406. int retval;
  407. unsigned long flags;
  408. spin_lock_irqsave(&data->lock, flags);
  409. /* validate Tx/Rx buffers and Transfer length */
  410. list_for_each_entry(transfer, &pmsg->transfers, transfer_list) {
  411. if (!transfer->tx_buf && !transfer->rx_buf) {
  412. dev_err(&pspi->dev,
  413. "%s Tx and Rx buffer NULL\n", __func__);
  414. retval = -EINVAL;
  415. goto err_return_spinlock;
  416. }
  417. if (!transfer->len) {
  418. dev_err(&pspi->dev, "%s Transfer length invalid\n",
  419. __func__);
  420. retval = -EINVAL;
  421. goto err_return_spinlock;
  422. }
  423. dev_dbg(&pspi->dev,
  424. "%s Tx/Rx buffer valid. Transfer length valid\n",
  425. __func__);
  426. }
  427. spin_unlock_irqrestore(&data->lock, flags);
  428. /* We won't process any messages if we have been asked to terminate */
  429. if (data->status == STATUS_EXITING) {
  430. dev_err(&pspi->dev, "%s status = STATUS_EXITING.\n", __func__);
  431. retval = -ESHUTDOWN;
  432. goto err_out;
  433. }
  434. /* If suspended ,return -EINVAL */
  435. if (data->board_dat->suspend_sts) {
  436. dev_err(&pspi->dev, "%s suspend; returning EINVAL\n", __func__);
  437. retval = -EINVAL;
  438. goto err_out;
  439. }
  440. /* set status of message */
  441. pmsg->actual_length = 0;
  442. dev_dbg(&pspi->dev, "%s - pmsg->status =%d\n", __func__, pmsg->status);
  443. pmsg->status = -EINPROGRESS;
  444. spin_lock_irqsave(&data->lock, flags);
  445. /* add message to queue */
  446. list_add_tail(&pmsg->queue, &data->queue);
  447. spin_unlock_irqrestore(&data->lock, flags);
  448. dev_dbg(&pspi->dev, "%s - Invoked list_add_tail\n", __func__);
  449. /* schedule work queue to run */
  450. queue_work(data->wk, &data->work);
  451. dev_dbg(&pspi->dev, "%s - Invoked queue work\n", __func__);
  452. retval = 0;
  453. err_out:
  454. dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
  455. return retval;
  456. err_return_spinlock:
  457. dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
  458. spin_unlock_irqrestore(&data->lock, flags);
  459. return retval;
  460. }
  461. static inline void pch_spi_select_chip(struct pch_spi_data *data,
  462. struct spi_device *pspi)
  463. {
  464. if (data->current_chip != NULL) {
  465. if (pspi->chip_select != data->n_curnt_chip) {
  466. dev_dbg(&pspi->dev, "%s : different slave\n", __func__);
  467. data->current_chip = NULL;
  468. }
  469. }
  470. data->current_chip = pspi;
  471. data->n_curnt_chip = data->current_chip->chip_select;
  472. dev_dbg(&pspi->dev, "%s :Invoking pch_spi_setup_transfer\n", __func__);
  473. pch_spi_setup_transfer(pspi);
  474. }
  475. static void pch_spi_set_tx(struct pch_spi_data *data, int *bpw)
  476. {
  477. int size;
  478. u32 n_writes;
  479. int j;
  480. struct spi_message *pmsg, *tmp;
  481. const u8 *tx_buf;
  482. const u16 *tx_sbuf;
  483. /* set baud rate if needed */
  484. if (data->cur_trans->speed_hz) {
  485. dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
  486. pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
  487. }
  488. /* set bits per word if needed */
  489. if (data->cur_trans->bits_per_word &&
  490. (data->current_msg->spi->bits_per_word != data->cur_trans->bits_per_word)) {
  491. dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
  492. pch_spi_set_bits_per_word(data->master,
  493. data->cur_trans->bits_per_word);
  494. *bpw = data->cur_trans->bits_per_word;
  495. } else {
  496. *bpw = data->current_msg->spi->bits_per_word;
  497. }
  498. /* reset Tx/Rx index */
  499. data->tx_index = 0;
  500. data->rx_index = 0;
  501. data->bpw_len = data->cur_trans->len / (*bpw / 8);
  502. /* find alloc size */
  503. size = data->cur_trans->len * sizeof(*data->pkt_tx_buff);
  504. /* allocate memory for pkt_tx_buff & pkt_rx_buffer */
  505. data->pkt_tx_buff = kzalloc(size, GFP_KERNEL);
  506. if (data->pkt_tx_buff != NULL) {
  507. data->pkt_rx_buff = kzalloc(size, GFP_KERNEL);
  508. if (!data->pkt_rx_buff)
  509. kfree(data->pkt_tx_buff);
  510. }
  511. if (!data->pkt_rx_buff) {
  512. /* flush queue and set status of all transfers to -ENOMEM */
  513. dev_err(&data->master->dev, "%s :kzalloc failed\n", __func__);
  514. list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
  515. pmsg->status = -ENOMEM;
  516. if (pmsg->complete)
  517. pmsg->complete(pmsg->context);
  518. /* delete from queue */
  519. list_del_init(&pmsg->queue);
  520. }
  521. return;
  522. }
  523. /* copy Tx Data */
  524. if (data->cur_trans->tx_buf != NULL) {
  525. if (*bpw == 8) {
  526. tx_buf = data->cur_trans->tx_buf;
  527. for (j = 0; j < data->bpw_len; j++)
  528. data->pkt_tx_buff[j] = *tx_buf++;
  529. } else {
  530. tx_sbuf = data->cur_trans->tx_buf;
  531. for (j = 0; j < data->bpw_len; j++)
  532. data->pkt_tx_buff[j] = *tx_sbuf++;
  533. }
  534. }
  535. /* if len greater than PCH_MAX_FIFO_DEPTH, write 16,else len bytes */
  536. n_writes = data->bpw_len;
  537. if (n_writes > PCH_MAX_FIFO_DEPTH)
  538. n_writes = PCH_MAX_FIFO_DEPTH;
  539. dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
  540. "0x2 to SSNXCR\n", __func__);
  541. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
  542. for (j = 0; j < n_writes; j++)
  543. pch_spi_writereg(data->master, PCH_SPDWR, data->pkt_tx_buff[j]);
  544. /* update tx_index */
  545. data->tx_index = j;
  546. /* reset transfer complete flag */
  547. data->transfer_complete = false;
  548. data->transfer_active = true;
  549. }
  550. static void pch_spi_nomore_transfer(struct pch_spi_data *data)
  551. {
  552. struct spi_message *pmsg, *tmp;
  553. dev_dbg(&data->master->dev, "%s called\n", __func__);
  554. /* Invoke complete callback
  555. * [To the spi core..indicating end of transfer] */
  556. data->current_msg->status = 0;
  557. if (data->current_msg->complete) {
  558. dev_dbg(&data->master->dev,
  559. "%s:Invoking callback of SPI core\n", __func__);
  560. data->current_msg->complete(data->current_msg->context);
  561. }
  562. /* update status in global variable */
  563. data->bcurrent_msg_processing = false;
  564. dev_dbg(&data->master->dev,
  565. "%s:data->bcurrent_msg_processing = false\n", __func__);
  566. data->current_msg = NULL;
  567. data->cur_trans = NULL;
  568. /* check if we have items in list and not suspending
  569. * return 1 if list empty */
  570. if ((list_empty(&data->queue) == 0) &&
  571. (!data->board_dat->suspend_sts) &&
  572. (data->status != STATUS_EXITING)) {
  573. /* We have some more work to do (either there is more tranint
  574. * bpw;sfer requests in the current message or there are
  575. *more messages)
  576. */
  577. dev_dbg(&data->master->dev, "%s:Invoke queue_work\n", __func__);
  578. queue_work(data->wk, &data->work);
  579. } else if (data->board_dat->suspend_sts ||
  580. data->status == STATUS_EXITING) {
  581. dev_dbg(&data->master->dev,
  582. "%s suspend/remove initiated, flushing queue\n",
  583. __func__);
  584. list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
  585. pmsg->status = -EIO;
  586. if (pmsg->complete)
  587. pmsg->complete(pmsg->context);
  588. /* delete from queue */
  589. list_del_init(&pmsg->queue);
  590. }
  591. }
  592. }
  593. static void pch_spi_set_ir(struct pch_spi_data *data)
  594. {
  595. /* enable interrupts, set threshold, enable SPI */
  596. if ((data->bpw_len) > PCH_MAX_FIFO_DEPTH)
  597. /* set receive threshold to PCH_RX_THOLD */
  598. pch_spi_setclr_reg(data->master, PCH_SPCR,
  599. PCH_RX_THOLD << SPCR_RFIC_FIELD |
  600. SPCR_FIE_BIT | SPCR_RFIE_BIT |
  601. SPCR_ORIE_BIT | SPCR_SPE_BIT,
  602. MASK_RFIC_SPCR_BITS | PCH_ALL);
  603. else
  604. /* set receive threshold to maximum */
  605. pch_spi_setclr_reg(data->master, PCH_SPCR,
  606. PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD |
  607. SPCR_FIE_BIT | SPCR_ORIE_BIT |
  608. SPCR_SPE_BIT,
  609. MASK_RFIC_SPCR_BITS | PCH_ALL);
  610. /* Wait until the transfer completes; go to sleep after
  611. initiating the transfer. */
  612. dev_dbg(&data->master->dev,
  613. "%s:waiting for transfer to get over\n", __func__);
  614. wait_event_interruptible(data->wait, data->transfer_complete);
  615. /* clear all interrupts */
  616. pch_spi_writereg(data->master, PCH_SPSR,
  617. pch_spi_readreg(data->master, PCH_SPSR));
  618. /* Disable interrupts and SPI transfer */
  619. pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL | SPCR_SPE_BIT);
  620. /* clear FIFO */
  621. pch_spi_clear_fifo(data->master);
  622. }
  623. static void pch_spi_copy_rx_data(struct pch_spi_data *data, int bpw)
  624. {
  625. int j;
  626. u8 *rx_buf;
  627. u16 *rx_sbuf;
  628. /* copy Rx Data */
  629. if (!data->cur_trans->rx_buf)
  630. return;
  631. if (bpw == 8) {
  632. rx_buf = data->cur_trans->rx_buf;
  633. for (j = 0; j < data->bpw_len; j++)
  634. *rx_buf++ = data->pkt_rx_buff[j] & 0xFF;
  635. } else {
  636. rx_sbuf = data->cur_trans->rx_buf;
  637. for (j = 0; j < data->bpw_len; j++)
  638. *rx_sbuf++ = data->pkt_rx_buff[j];
  639. }
  640. }
  641. static void pch_spi_copy_rx_data_for_dma(struct pch_spi_data *data, int bpw)
  642. {
  643. int j;
  644. u8 *rx_buf;
  645. u16 *rx_sbuf;
  646. const u8 *rx_dma_buf;
  647. const u16 *rx_dma_sbuf;
  648. /* copy Rx Data */
  649. if (!data->cur_trans->rx_buf)
  650. return;
  651. if (bpw == 8) {
  652. rx_buf = data->cur_trans->rx_buf;
  653. rx_dma_buf = data->dma.rx_buf_virt;
  654. for (j = 0; j < data->bpw_len; j++)
  655. *rx_buf++ = *rx_dma_buf++ & 0xFF;
  656. data->cur_trans->rx_buf = rx_buf;
  657. } else {
  658. rx_sbuf = data->cur_trans->rx_buf;
  659. rx_dma_sbuf = data->dma.rx_buf_virt;
  660. for (j = 0; j < data->bpw_len; j++)
  661. *rx_sbuf++ = *rx_dma_sbuf++;
  662. data->cur_trans->rx_buf = rx_sbuf;
  663. }
  664. }
  665. static int pch_spi_start_transfer(struct pch_spi_data *data)
  666. {
  667. struct pch_spi_dma_ctrl *dma;
  668. unsigned long flags;
  669. int rtn;
  670. dma = &data->dma;
  671. spin_lock_irqsave(&data->lock, flags);
  672. /* disable interrupts, SPI set enable */
  673. pch_spi_setclr_reg(data->master, PCH_SPCR, SPCR_SPE_BIT, PCH_ALL);
  674. spin_unlock_irqrestore(&data->lock, flags);
  675. /* Wait until the transfer completes; go to sleep after
  676. initiating the transfer. */
  677. dev_dbg(&data->master->dev,
  678. "%s:waiting for transfer to get over\n", __func__);
  679. rtn = wait_event_interruptible_timeout(data->wait,
  680. data->transfer_complete,
  681. msecs_to_jiffies(2 * HZ));
  682. if (!rtn)
  683. dev_err(&data->master->dev,
  684. "%s wait-event timeout\n", __func__);
  685. dma_sync_sg_for_cpu(&data->master->dev, dma->sg_rx_p, dma->nent,
  686. DMA_FROM_DEVICE);
  687. dma_sync_sg_for_cpu(&data->master->dev, dma->sg_tx_p, dma->nent,
  688. DMA_FROM_DEVICE);
  689. memset(data->dma.tx_buf_virt, 0, PAGE_SIZE);
  690. async_tx_ack(dma->desc_rx);
  691. async_tx_ack(dma->desc_tx);
  692. kfree(dma->sg_tx_p);
  693. kfree(dma->sg_rx_p);
  694. spin_lock_irqsave(&data->lock, flags);
  695. /* clear fifo threshold, disable interrupts, disable SPI transfer */
  696. pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
  697. MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS | PCH_ALL |
  698. SPCR_SPE_BIT);
  699. /* clear all interrupts */
  700. pch_spi_writereg(data->master, PCH_SPSR,
  701. pch_spi_readreg(data->master, PCH_SPSR));
  702. /* clear FIFO */
  703. pch_spi_clear_fifo(data->master);
  704. spin_unlock_irqrestore(&data->lock, flags);
  705. return rtn;
  706. }
  707. static void pch_dma_rx_complete(void *arg)
  708. {
  709. struct pch_spi_data *data = arg;
  710. /* transfer is completed;inform pch_spi_process_messages_dma */
  711. data->transfer_complete = true;
  712. wake_up_interruptible(&data->wait);
  713. }
  714. static bool pch_spi_filter(struct dma_chan *chan, void *slave)
  715. {
  716. struct pch_dma_slave *param = slave;
  717. if ((chan->chan_id == param->chan_id) &&
  718. (param->dma_dev == chan->device->dev)) {
  719. chan->private = param;
  720. return true;
  721. } else {
  722. return false;
  723. }
  724. }
  725. static void pch_spi_request_dma(struct pch_spi_data *data, int bpw)
  726. {
  727. dma_cap_mask_t mask;
  728. struct dma_chan *chan;
  729. struct pci_dev *dma_dev;
  730. struct pch_dma_slave *param;
  731. struct pch_spi_dma_ctrl *dma;
  732. unsigned int width;
  733. if (bpw == 8)
  734. width = PCH_DMA_WIDTH_1_BYTE;
  735. else
  736. width = PCH_DMA_WIDTH_2_BYTES;
  737. dma = &data->dma;
  738. dma_cap_zero(mask);
  739. dma_cap_set(DMA_SLAVE, mask);
  740. /* Get DMA's dev information */
  741. dma_dev = pci_get_slot(data->board_dat->pdev->bus,
  742. PCI_DEVFN(PCI_SLOT(data->board_dat->pdev->devfn), 0));
  743. /* Set Tx DMA */
  744. param = &dma->param_tx;
  745. param->dma_dev = &dma_dev->dev;
  746. param->chan_id = data->ch * 2; /* Tx = 0, 2 */;
  747. param->tx_reg = data->io_base_addr + PCH_SPDWR;
  748. param->width = width;
  749. chan = dma_request_channel(mask, pch_spi_filter, param);
  750. if (!chan) {
  751. dev_err(&data->master->dev,
  752. "ERROR: dma_request_channel FAILS(Tx)\n");
  753. data->use_dma = 0;
  754. return;
  755. }
  756. dma->chan_tx = chan;
  757. /* Set Rx DMA */
  758. param = &dma->param_rx;
  759. param->dma_dev = &dma_dev->dev;
  760. param->chan_id = data->ch * 2 + 1; /* Rx = Tx + 1 */;
  761. param->rx_reg = data->io_base_addr + PCH_SPDRR;
  762. param->width = width;
  763. chan = dma_request_channel(mask, pch_spi_filter, param);
  764. if (!chan) {
  765. dev_err(&data->master->dev,
  766. "ERROR: dma_request_channel FAILS(Rx)\n");
  767. dma_release_channel(dma->chan_tx);
  768. dma->chan_tx = NULL;
  769. data->use_dma = 0;
  770. return;
  771. }
  772. dma->chan_rx = chan;
  773. }
  774. static void pch_spi_release_dma(struct pch_spi_data *data)
  775. {
  776. struct pch_spi_dma_ctrl *dma;
  777. dma = &data->dma;
  778. if (dma->chan_tx) {
  779. dma_release_channel(dma->chan_tx);
  780. dma->chan_tx = NULL;
  781. }
  782. if (dma->chan_rx) {
  783. dma_release_channel(dma->chan_rx);
  784. dma->chan_rx = NULL;
  785. }
  786. return;
  787. }
  788. static void pch_spi_handle_dma(struct pch_spi_data *data, int *bpw)
  789. {
  790. const u8 *tx_buf;
  791. const u16 *tx_sbuf;
  792. u8 *tx_dma_buf;
  793. u16 *tx_dma_sbuf;
  794. struct scatterlist *sg;
  795. struct dma_async_tx_descriptor *desc_tx;
  796. struct dma_async_tx_descriptor *desc_rx;
  797. int num;
  798. int i;
  799. int size;
  800. int rem;
  801. int head;
  802. unsigned long flags;
  803. struct pch_spi_dma_ctrl *dma;
  804. dma = &data->dma;
  805. /* set baud rate if needed */
  806. if (data->cur_trans->speed_hz) {
  807. dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
  808. spin_lock_irqsave(&data->lock, flags);
  809. pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
  810. spin_unlock_irqrestore(&data->lock, flags);
  811. }
  812. /* set bits per word if needed */
  813. if (data->cur_trans->bits_per_word &&
  814. (data->current_msg->spi->bits_per_word !=
  815. data->cur_trans->bits_per_word)) {
  816. dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
  817. spin_lock_irqsave(&data->lock, flags);
  818. pch_spi_set_bits_per_word(data->master,
  819. data->cur_trans->bits_per_word);
  820. spin_unlock_irqrestore(&data->lock, flags);
  821. *bpw = data->cur_trans->bits_per_word;
  822. } else {
  823. *bpw = data->current_msg->spi->bits_per_word;
  824. }
  825. data->bpw_len = data->cur_trans->len / (*bpw / 8);
  826. if (data->bpw_len > PCH_BUF_SIZE) {
  827. data->bpw_len = PCH_BUF_SIZE;
  828. data->cur_trans->len -= PCH_BUF_SIZE;
  829. }
  830. /* copy Tx Data */
  831. if (data->cur_trans->tx_buf != NULL) {
  832. if (*bpw == 8) {
  833. tx_buf = data->cur_trans->tx_buf;
  834. tx_dma_buf = dma->tx_buf_virt;
  835. for (i = 0; i < data->bpw_len; i++)
  836. *tx_dma_buf++ = *tx_buf++;
  837. } else {
  838. tx_sbuf = data->cur_trans->tx_buf;
  839. tx_dma_sbuf = dma->tx_buf_virt;
  840. for (i = 0; i < data->bpw_len; i++)
  841. *tx_dma_sbuf++ = *tx_sbuf++;
  842. }
  843. }
  844. /* Calculate Rx parameter for DMA transmitting */
  845. if (data->bpw_len > PCH_DMA_TRANS_SIZE) {
  846. if (data->bpw_len % PCH_DMA_TRANS_SIZE) {
  847. num = data->bpw_len / PCH_DMA_TRANS_SIZE + 1;
  848. rem = data->bpw_len % PCH_DMA_TRANS_SIZE;
  849. } else {
  850. num = data->bpw_len / PCH_DMA_TRANS_SIZE;
  851. rem = PCH_DMA_TRANS_SIZE;
  852. }
  853. size = PCH_DMA_TRANS_SIZE;
  854. } else {
  855. num = 1;
  856. size = data->bpw_len;
  857. rem = data->bpw_len;
  858. }
  859. dev_dbg(&data->master->dev, "%s num=%d size=%d rem=%d\n",
  860. __func__, num, size, rem);
  861. spin_lock_irqsave(&data->lock, flags);
  862. /* set receive fifo threshold and transmit fifo threshold */
  863. pch_spi_setclr_reg(data->master, PCH_SPCR,
  864. ((size - 1) << SPCR_RFIC_FIELD) |
  865. (PCH_TX_THOLD << SPCR_TFIC_FIELD),
  866. MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS);
  867. spin_unlock_irqrestore(&data->lock, flags);
  868. /* RX */
  869. dma->sg_rx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  870. sg_init_table(dma->sg_rx_p, num); /* Initialize SG table */
  871. /* offset, length setting */
  872. sg = dma->sg_rx_p;
  873. for (i = 0; i < num; i++, sg++) {
  874. if (i == (num - 2)) {
  875. sg->offset = size * i;
  876. sg->offset = sg->offset * (*bpw / 8);
  877. sg_set_page(sg, virt_to_page(dma->rx_buf_virt), rem,
  878. sg->offset);
  879. sg_dma_len(sg) = rem;
  880. } else if (i == (num - 1)) {
  881. sg->offset = size * (i - 1) + rem;
  882. sg->offset = sg->offset * (*bpw / 8);
  883. sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
  884. sg->offset);
  885. sg_dma_len(sg) = size;
  886. } else {
  887. sg->offset = size * i;
  888. sg->offset = sg->offset * (*bpw / 8);
  889. sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
  890. sg->offset);
  891. sg_dma_len(sg) = size;
  892. }
  893. sg_dma_address(sg) = dma->rx_buf_dma + sg->offset;
  894. }
  895. sg = dma->sg_rx_p;
  896. desc_rx = dmaengine_prep_slave_sg(dma->chan_rx, sg,
  897. num, DMA_DEV_TO_MEM,
  898. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  899. if (!desc_rx) {
  900. dev_err(&data->master->dev,
  901. "%s:dmaengine_prep_slave_sg Failed\n", __func__);
  902. return;
  903. }
  904. dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_FROM_DEVICE);
  905. desc_rx->callback = pch_dma_rx_complete;
  906. desc_rx->callback_param = data;
  907. dma->nent = num;
  908. dma->desc_rx = desc_rx;
  909. /* Calculate Tx parameter for DMA transmitting */
  910. if (data->bpw_len > PCH_MAX_FIFO_DEPTH) {
  911. head = PCH_MAX_FIFO_DEPTH - PCH_DMA_TRANS_SIZE;
  912. if (data->bpw_len % PCH_DMA_TRANS_SIZE > 4) {
  913. num = data->bpw_len / PCH_DMA_TRANS_SIZE + 1;
  914. rem = data->bpw_len % PCH_DMA_TRANS_SIZE - head;
  915. } else {
  916. num = data->bpw_len / PCH_DMA_TRANS_SIZE;
  917. rem = data->bpw_len % PCH_DMA_TRANS_SIZE +
  918. PCH_DMA_TRANS_SIZE - head;
  919. }
  920. size = PCH_DMA_TRANS_SIZE;
  921. } else {
  922. num = 1;
  923. size = data->bpw_len;
  924. rem = data->bpw_len;
  925. head = 0;
  926. }
  927. dma->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  928. sg_init_table(dma->sg_tx_p, num); /* Initialize SG table */
  929. /* offset, length setting */
  930. sg = dma->sg_tx_p;
  931. for (i = 0; i < num; i++, sg++) {
  932. if (i == 0) {
  933. sg->offset = 0;
  934. sg_set_page(sg, virt_to_page(dma->tx_buf_virt), size + head,
  935. sg->offset);
  936. sg_dma_len(sg) = size + head;
  937. } else if (i == (num - 1)) {
  938. sg->offset = head + size * i;
  939. sg->offset = sg->offset * (*bpw / 8);
  940. sg_set_page(sg, virt_to_page(dma->tx_buf_virt), rem,
  941. sg->offset);
  942. sg_dma_len(sg) = rem;
  943. } else {
  944. sg->offset = head + size * i;
  945. sg->offset = sg->offset * (*bpw / 8);
  946. sg_set_page(sg, virt_to_page(dma->tx_buf_virt), size,
  947. sg->offset);
  948. sg_dma_len(sg) = size;
  949. }
  950. sg_dma_address(sg) = dma->tx_buf_dma + sg->offset;
  951. }
  952. sg = dma->sg_tx_p;
  953. desc_tx = dmaengine_prep_slave_sg(dma->chan_tx,
  954. sg, num, DMA_MEM_TO_DEV,
  955. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  956. if (!desc_tx) {
  957. dev_err(&data->master->dev,
  958. "%s:dmaengine_prep_slave_sg Failed\n", __func__);
  959. return;
  960. }
  961. dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_TO_DEVICE);
  962. desc_tx->callback = NULL;
  963. desc_tx->callback_param = data;
  964. dma->nent = num;
  965. dma->desc_tx = desc_tx;
  966. dev_dbg(&data->master->dev, "%s:Pulling down SSN low - writing 0x2 to SSNXCR\n", __func__);
  967. spin_lock_irqsave(&data->lock, flags);
  968. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
  969. desc_rx->tx_submit(desc_rx);
  970. desc_tx->tx_submit(desc_tx);
  971. spin_unlock_irqrestore(&data->lock, flags);
  972. /* reset transfer complete flag */
  973. data->transfer_complete = false;
  974. }
  975. static void pch_spi_process_messages(struct work_struct *pwork)
  976. {
  977. struct spi_message *pmsg, *tmp;
  978. struct pch_spi_data *data;
  979. int bpw;
  980. data = container_of(pwork, struct pch_spi_data, work);
  981. dev_dbg(&data->master->dev, "%s data initialized\n", __func__);
  982. spin_lock(&data->lock);
  983. /* check if suspend has been initiated;if yes flush queue */
  984. if (data->board_dat->suspend_sts || (data->status == STATUS_EXITING)) {
  985. dev_dbg(&data->master->dev,
  986. "%s suspend/remove initiated, flushing queue\n", __func__);
  987. list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
  988. pmsg->status = -EIO;
  989. if (pmsg->complete) {
  990. spin_unlock(&data->lock);
  991. pmsg->complete(pmsg->context);
  992. spin_lock(&data->lock);
  993. }
  994. /* delete from queue */
  995. list_del_init(&pmsg->queue);
  996. }
  997. spin_unlock(&data->lock);
  998. return;
  999. }
  1000. data->bcurrent_msg_processing = true;
  1001. dev_dbg(&data->master->dev,
  1002. "%s Set data->bcurrent_msg_processing= true\n", __func__);
  1003. /* Get the message from the queue and delete it from there. */
  1004. data->current_msg = list_entry(data->queue.next, struct spi_message,
  1005. queue);
  1006. list_del_init(&data->current_msg->queue);
  1007. data->current_msg->status = 0;
  1008. pch_spi_select_chip(data, data->current_msg->spi);
  1009. spin_unlock(&data->lock);
  1010. if (data->use_dma)
  1011. pch_spi_request_dma(data,
  1012. data->current_msg->spi->bits_per_word);
  1013. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_NO_CONTROL);
  1014. do {
  1015. int cnt;
  1016. /* If we are already processing a message get the next
  1017. transfer structure from the message otherwise retrieve
  1018. the 1st transfer request from the message. */
  1019. spin_lock(&data->lock);
  1020. if (data->cur_trans == NULL) {
  1021. data->cur_trans =
  1022. list_entry(data->current_msg->transfers.next,
  1023. struct spi_transfer, transfer_list);
  1024. dev_dbg(&data->master->dev, "%s "
  1025. ":Getting 1st transfer message\n", __func__);
  1026. } else {
  1027. data->cur_trans =
  1028. list_entry(data->cur_trans->transfer_list.next,
  1029. struct spi_transfer, transfer_list);
  1030. dev_dbg(&data->master->dev, "%s "
  1031. ":Getting next transfer message\n", __func__);
  1032. }
  1033. spin_unlock(&data->lock);
  1034. if (!data->cur_trans->len)
  1035. goto out;
  1036. cnt = (data->cur_trans->len - 1) / PCH_BUF_SIZE + 1;
  1037. data->save_total_len = data->cur_trans->len;
  1038. if (data->use_dma) {
  1039. int i;
  1040. char *save_rx_buf = data->cur_trans->rx_buf;
  1041. for (i = 0; i < cnt; i ++) {
  1042. pch_spi_handle_dma(data, &bpw);
  1043. if (!pch_spi_start_transfer(data)) {
  1044. data->transfer_complete = true;
  1045. data->current_msg->status = -EIO;
  1046. data->current_msg->complete
  1047. (data->current_msg->context);
  1048. data->bcurrent_msg_processing = false;
  1049. data->current_msg = NULL;
  1050. data->cur_trans = NULL;
  1051. goto out;
  1052. }
  1053. pch_spi_copy_rx_data_for_dma(data, bpw);
  1054. }
  1055. data->cur_trans->rx_buf = save_rx_buf;
  1056. } else {
  1057. pch_spi_set_tx(data, &bpw);
  1058. pch_spi_set_ir(data);
  1059. pch_spi_copy_rx_data(data, bpw);
  1060. kfree(data->pkt_rx_buff);
  1061. data->pkt_rx_buff = NULL;
  1062. kfree(data->pkt_tx_buff);
  1063. data->pkt_tx_buff = NULL;
  1064. }
  1065. /* increment message count */
  1066. data->cur_trans->len = data->save_total_len;
  1067. data->current_msg->actual_length += data->cur_trans->len;
  1068. dev_dbg(&data->master->dev,
  1069. "%s:data->current_msg->actual_length=%d\n",
  1070. __func__, data->current_msg->actual_length);
  1071. /* check for delay */
  1072. if (data->cur_trans->delay_usecs) {
  1073. dev_dbg(&data->master->dev, "%s:"
  1074. "delay in usec=%d\n", __func__,
  1075. data->cur_trans->delay_usecs);
  1076. udelay(data->cur_trans->delay_usecs);
  1077. }
  1078. spin_lock(&data->lock);
  1079. /* No more transfer in this message. */
  1080. if ((data->cur_trans->transfer_list.next) ==
  1081. &(data->current_msg->transfers)) {
  1082. pch_spi_nomore_transfer(data);
  1083. }
  1084. spin_unlock(&data->lock);
  1085. } while (data->cur_trans != NULL);
  1086. out:
  1087. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_HIGH);
  1088. if (data->use_dma)
  1089. pch_spi_release_dma(data);
  1090. }
  1091. static void pch_spi_free_resources(struct pch_spi_board_data *board_dat,
  1092. struct pch_spi_data *data)
  1093. {
  1094. dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
  1095. /* free workqueue */
  1096. if (data->wk != NULL) {
  1097. destroy_workqueue(data->wk);
  1098. data->wk = NULL;
  1099. dev_dbg(&board_dat->pdev->dev,
  1100. "%s destroy_workqueue invoked successfully\n",
  1101. __func__);
  1102. }
  1103. }
  1104. static int pch_spi_get_resources(struct pch_spi_board_data *board_dat,
  1105. struct pch_spi_data *data)
  1106. {
  1107. int retval = 0;
  1108. dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
  1109. /* create workqueue */
  1110. data->wk = create_singlethread_workqueue(KBUILD_MODNAME);
  1111. if (!data->wk) {
  1112. dev_err(&board_dat->pdev->dev,
  1113. "%s create_singlet hread_workqueue failed\n", __func__);
  1114. retval = -EBUSY;
  1115. goto err_return;
  1116. }
  1117. /* reset PCH SPI h/w */
  1118. pch_spi_reset(data->master);
  1119. dev_dbg(&board_dat->pdev->dev,
  1120. "%s pch_spi_reset invoked successfully\n", __func__);
  1121. dev_dbg(&board_dat->pdev->dev, "%s data->irq_reg_sts=true\n", __func__);
  1122. err_return:
  1123. if (retval != 0) {
  1124. dev_err(&board_dat->pdev->dev,
  1125. "%s FAIL:invoking pch_spi_free_resources\n", __func__);
  1126. pch_spi_free_resources(board_dat, data);
  1127. }
  1128. dev_dbg(&board_dat->pdev->dev, "%s Return=%d\n", __func__, retval);
  1129. return retval;
  1130. }
  1131. static void pch_free_dma_buf(struct pch_spi_board_data *board_dat,
  1132. struct pch_spi_data *data)
  1133. {
  1134. struct pch_spi_dma_ctrl *dma;
  1135. dma = &data->dma;
  1136. if (dma->tx_buf_dma)
  1137. dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
  1138. dma->tx_buf_virt, dma->tx_buf_dma);
  1139. if (dma->rx_buf_dma)
  1140. dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
  1141. dma->rx_buf_virt, dma->rx_buf_dma);
  1142. return;
  1143. }
  1144. static void pch_alloc_dma_buf(struct pch_spi_board_data *board_dat,
  1145. struct pch_spi_data *data)
  1146. {
  1147. struct pch_spi_dma_ctrl *dma;
  1148. dma = &data->dma;
  1149. /* Get Consistent memory for Tx DMA */
  1150. dma->tx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
  1151. PCH_BUF_SIZE, &dma->tx_buf_dma, GFP_KERNEL);
  1152. /* Get Consistent memory for Rx DMA */
  1153. dma->rx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
  1154. PCH_BUF_SIZE, &dma->rx_buf_dma, GFP_KERNEL);
  1155. }
  1156. static int pch_spi_pd_probe(struct platform_device *plat_dev)
  1157. {
  1158. int ret;
  1159. struct spi_master *master;
  1160. struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
  1161. struct pch_spi_data *data;
  1162. dev_dbg(&plat_dev->dev, "%s:debug\n", __func__);
  1163. master = spi_alloc_master(&board_dat->pdev->dev,
  1164. sizeof(struct pch_spi_data));
  1165. if (!master) {
  1166. dev_err(&plat_dev->dev, "spi_alloc_master[%d] failed.\n",
  1167. plat_dev->id);
  1168. return -ENOMEM;
  1169. }
  1170. data = spi_master_get_devdata(master);
  1171. data->master = master;
  1172. platform_set_drvdata(plat_dev, data);
  1173. /* baseaddress + address offset) */
  1174. data->io_base_addr = pci_resource_start(board_dat->pdev, 1) +
  1175. PCH_ADDRESS_SIZE * plat_dev->id;
  1176. data->io_remap_addr = pci_iomap(board_dat->pdev, 1, 0);
  1177. if (!data->io_remap_addr) {
  1178. dev_err(&plat_dev->dev, "%s pci_iomap failed\n", __func__);
  1179. ret = -ENOMEM;
  1180. goto err_pci_iomap;
  1181. }
  1182. data->io_remap_addr += PCH_ADDRESS_SIZE * plat_dev->id;
  1183. dev_dbg(&plat_dev->dev, "[ch%d] remap_addr=%p\n",
  1184. plat_dev->id, data->io_remap_addr);
  1185. /* initialize members of SPI master */
  1186. master->num_chipselect = PCH_MAX_CS;
  1187. master->transfer = pch_spi_transfer;
  1188. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  1189. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  1190. master->max_speed_hz = PCH_MAX_BAUDRATE;
  1191. data->board_dat = board_dat;
  1192. data->plat_dev = plat_dev;
  1193. data->n_curnt_chip = 255;
  1194. data->status = STATUS_RUNNING;
  1195. data->ch = plat_dev->id;
  1196. data->use_dma = use_dma;
  1197. INIT_LIST_HEAD(&data->queue);
  1198. spin_lock_init(&data->lock);
  1199. INIT_WORK(&data->work, pch_spi_process_messages);
  1200. init_waitqueue_head(&data->wait);
  1201. ret = pch_spi_get_resources(board_dat, data);
  1202. if (ret) {
  1203. dev_err(&plat_dev->dev, "%s fail(retval=%d)\n", __func__, ret);
  1204. goto err_spi_get_resources;
  1205. }
  1206. ret = request_irq(board_dat->pdev->irq, pch_spi_handler,
  1207. IRQF_SHARED, KBUILD_MODNAME, data);
  1208. if (ret) {
  1209. dev_err(&plat_dev->dev,
  1210. "%s request_irq failed\n", __func__);
  1211. goto err_request_irq;
  1212. }
  1213. data->irq_reg_sts = true;
  1214. pch_spi_set_master_mode(master);
  1215. if (use_dma) {
  1216. dev_info(&plat_dev->dev, "Use DMA for data transfers\n");
  1217. pch_alloc_dma_buf(board_dat, data);
  1218. }
  1219. ret = spi_register_master(master);
  1220. if (ret != 0) {
  1221. dev_err(&plat_dev->dev,
  1222. "%s spi_register_master FAILED\n", __func__);
  1223. goto err_spi_register_master;
  1224. }
  1225. return 0;
  1226. err_spi_register_master:
  1227. pch_free_dma_buf(board_dat, data);
  1228. free_irq(board_dat->pdev->irq, data);
  1229. err_request_irq:
  1230. pch_spi_free_resources(board_dat, data);
  1231. err_spi_get_resources:
  1232. pci_iounmap(board_dat->pdev, data->io_remap_addr);
  1233. err_pci_iomap:
  1234. spi_master_put(master);
  1235. return ret;
  1236. }
  1237. static int pch_spi_pd_remove(struct platform_device *plat_dev)
  1238. {
  1239. struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
  1240. struct pch_spi_data *data = platform_get_drvdata(plat_dev);
  1241. int count;
  1242. unsigned long flags;
  1243. dev_dbg(&plat_dev->dev, "%s:[ch%d] irq=%d\n",
  1244. __func__, plat_dev->id, board_dat->pdev->irq);
  1245. if (use_dma)
  1246. pch_free_dma_buf(board_dat, data);
  1247. /* check for any pending messages; no action is taken if the queue
  1248. * is still full; but at least we tried. Unload anyway */
  1249. count = 500;
  1250. spin_lock_irqsave(&data->lock, flags);
  1251. data->status = STATUS_EXITING;
  1252. while ((list_empty(&data->queue) == 0) && --count) {
  1253. dev_dbg(&board_dat->pdev->dev, "%s :queue not empty\n",
  1254. __func__);
  1255. spin_unlock_irqrestore(&data->lock, flags);
  1256. msleep(PCH_SLEEP_TIME);
  1257. spin_lock_irqsave(&data->lock, flags);
  1258. }
  1259. spin_unlock_irqrestore(&data->lock, flags);
  1260. pch_spi_free_resources(board_dat, data);
  1261. /* disable interrupts & free IRQ */
  1262. if (data->irq_reg_sts) {
  1263. /* disable interrupts */
  1264. pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
  1265. data->irq_reg_sts = false;
  1266. free_irq(board_dat->pdev->irq, data);
  1267. }
  1268. pci_iounmap(board_dat->pdev, data->io_remap_addr);
  1269. spi_unregister_master(data->master);
  1270. return 0;
  1271. }
  1272. #ifdef CONFIG_PM
  1273. static int pch_spi_pd_suspend(struct platform_device *pd_dev,
  1274. pm_message_t state)
  1275. {
  1276. u8 count;
  1277. struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
  1278. struct pch_spi_data *data = platform_get_drvdata(pd_dev);
  1279. dev_dbg(&pd_dev->dev, "%s ENTRY\n", __func__);
  1280. if (!board_dat) {
  1281. dev_err(&pd_dev->dev,
  1282. "%s pci_get_drvdata returned NULL\n", __func__);
  1283. return -EFAULT;
  1284. }
  1285. /* check if the current message is processed:
  1286. Only after thats done the transfer will be suspended */
  1287. count = 255;
  1288. while ((--count) > 0) {
  1289. if (!(data->bcurrent_msg_processing))
  1290. break;
  1291. msleep(PCH_SLEEP_TIME);
  1292. }
  1293. /* Free IRQ */
  1294. if (data->irq_reg_sts) {
  1295. /* disable all interrupts */
  1296. pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
  1297. pch_spi_reset(data->master);
  1298. free_irq(board_dat->pdev->irq, data);
  1299. data->irq_reg_sts = false;
  1300. dev_dbg(&pd_dev->dev,
  1301. "%s free_irq invoked successfully.\n", __func__);
  1302. }
  1303. return 0;
  1304. }
  1305. static int pch_spi_pd_resume(struct platform_device *pd_dev)
  1306. {
  1307. struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
  1308. struct pch_spi_data *data = platform_get_drvdata(pd_dev);
  1309. int retval;
  1310. if (!board_dat) {
  1311. dev_err(&pd_dev->dev,
  1312. "%s pci_get_drvdata returned NULL\n", __func__);
  1313. return -EFAULT;
  1314. }
  1315. if (!data->irq_reg_sts) {
  1316. /* register IRQ */
  1317. retval = request_irq(board_dat->pdev->irq, pch_spi_handler,
  1318. IRQF_SHARED, KBUILD_MODNAME, data);
  1319. if (retval < 0) {
  1320. dev_err(&pd_dev->dev,
  1321. "%s request_irq failed\n", __func__);
  1322. return retval;
  1323. }
  1324. /* reset PCH SPI h/w */
  1325. pch_spi_reset(data->master);
  1326. pch_spi_set_master_mode(data->master);
  1327. data->irq_reg_sts = true;
  1328. }
  1329. return 0;
  1330. }
  1331. #else
  1332. #define pch_spi_pd_suspend NULL
  1333. #define pch_spi_pd_resume NULL
  1334. #endif
  1335. static struct platform_driver pch_spi_pd_driver = {
  1336. .driver = {
  1337. .name = "pch-spi",
  1338. },
  1339. .probe = pch_spi_pd_probe,
  1340. .remove = pch_spi_pd_remove,
  1341. .suspend = pch_spi_pd_suspend,
  1342. .resume = pch_spi_pd_resume
  1343. };
  1344. static int pch_spi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1345. {
  1346. struct pch_spi_board_data *board_dat;
  1347. struct platform_device *pd_dev = NULL;
  1348. int retval;
  1349. int i;
  1350. struct pch_pd_dev_save *pd_dev_save;
  1351. pd_dev_save = kzalloc(sizeof(struct pch_pd_dev_save), GFP_KERNEL);
  1352. if (!pd_dev_save)
  1353. return -ENOMEM;
  1354. board_dat = kzalloc(sizeof(struct pch_spi_board_data), GFP_KERNEL);
  1355. if (!board_dat) {
  1356. retval = -ENOMEM;
  1357. goto err_no_mem;
  1358. }
  1359. retval = pci_request_regions(pdev, KBUILD_MODNAME);
  1360. if (retval) {
  1361. dev_err(&pdev->dev, "%s request_region failed\n", __func__);
  1362. goto pci_request_regions;
  1363. }
  1364. board_dat->pdev = pdev;
  1365. board_dat->num = id->driver_data;
  1366. pd_dev_save->num = id->driver_data;
  1367. pd_dev_save->board_dat = board_dat;
  1368. retval = pci_enable_device(pdev);
  1369. if (retval) {
  1370. dev_err(&pdev->dev, "%s pci_enable_device failed\n", __func__);
  1371. goto pci_enable_device;
  1372. }
  1373. for (i = 0; i < board_dat->num; i++) {
  1374. pd_dev = platform_device_alloc("pch-spi", i);
  1375. if (!pd_dev) {
  1376. dev_err(&pdev->dev, "platform_device_alloc failed\n");
  1377. retval = -ENOMEM;
  1378. goto err_platform_device;
  1379. }
  1380. pd_dev_save->pd_save[i] = pd_dev;
  1381. pd_dev->dev.parent = &pdev->dev;
  1382. retval = platform_device_add_data(pd_dev, board_dat,
  1383. sizeof(*board_dat));
  1384. if (retval) {
  1385. dev_err(&pdev->dev,
  1386. "platform_device_add_data failed\n");
  1387. platform_device_put(pd_dev);
  1388. goto err_platform_device;
  1389. }
  1390. retval = platform_device_add(pd_dev);
  1391. if (retval) {
  1392. dev_err(&pdev->dev, "platform_device_add failed\n");
  1393. platform_device_put(pd_dev);
  1394. goto err_platform_device;
  1395. }
  1396. }
  1397. pci_set_drvdata(pdev, pd_dev_save);
  1398. return 0;
  1399. err_platform_device:
  1400. while (--i >= 0)
  1401. platform_device_unregister(pd_dev_save->pd_save[i]);
  1402. pci_disable_device(pdev);
  1403. pci_enable_device:
  1404. pci_release_regions(pdev);
  1405. pci_request_regions:
  1406. kfree(board_dat);
  1407. err_no_mem:
  1408. kfree(pd_dev_save);
  1409. return retval;
  1410. }
  1411. static void pch_spi_remove(struct pci_dev *pdev)
  1412. {
  1413. int i;
  1414. struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
  1415. dev_dbg(&pdev->dev, "%s ENTRY:pdev=%p\n", __func__, pdev);
  1416. for (i = 0; i < pd_dev_save->num; i++)
  1417. platform_device_unregister(pd_dev_save->pd_save[i]);
  1418. pci_disable_device(pdev);
  1419. pci_release_regions(pdev);
  1420. kfree(pd_dev_save->board_dat);
  1421. kfree(pd_dev_save);
  1422. }
  1423. #ifdef CONFIG_PM
  1424. static int pch_spi_suspend(struct pci_dev *pdev, pm_message_t state)
  1425. {
  1426. int retval;
  1427. struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
  1428. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  1429. pd_dev_save->board_dat->suspend_sts = true;
  1430. /* save config space */
  1431. retval = pci_save_state(pdev);
  1432. if (retval == 0) {
  1433. pci_enable_wake(pdev, PCI_D3hot, 0);
  1434. pci_disable_device(pdev);
  1435. pci_set_power_state(pdev, PCI_D3hot);
  1436. } else {
  1437. dev_err(&pdev->dev, "%s pci_save_state failed\n", __func__);
  1438. }
  1439. return retval;
  1440. }
  1441. static int pch_spi_resume(struct pci_dev *pdev)
  1442. {
  1443. int retval;
  1444. struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
  1445. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  1446. pci_set_power_state(pdev, PCI_D0);
  1447. pci_restore_state(pdev);
  1448. retval = pci_enable_device(pdev);
  1449. if (retval < 0) {
  1450. dev_err(&pdev->dev,
  1451. "%s pci_enable_device failed\n", __func__);
  1452. } else {
  1453. pci_enable_wake(pdev, PCI_D3hot, 0);
  1454. /* set suspend status to false */
  1455. pd_dev_save->board_dat->suspend_sts = false;
  1456. }
  1457. return retval;
  1458. }
  1459. #else
  1460. #define pch_spi_suspend NULL
  1461. #define pch_spi_resume NULL
  1462. #endif
  1463. static struct pci_driver pch_spi_pcidev_driver = {
  1464. .name = "pch_spi",
  1465. .id_table = pch_spi_pcidev_id,
  1466. .probe = pch_spi_probe,
  1467. .remove = pch_spi_remove,
  1468. .suspend = pch_spi_suspend,
  1469. .resume = pch_spi_resume,
  1470. };
  1471. static int __init pch_spi_init(void)
  1472. {
  1473. int ret;
  1474. ret = platform_driver_register(&pch_spi_pd_driver);
  1475. if (ret)
  1476. return ret;
  1477. ret = pci_register_driver(&pch_spi_pcidev_driver);
  1478. if (ret) {
  1479. platform_driver_unregister(&pch_spi_pd_driver);
  1480. return ret;
  1481. }
  1482. return 0;
  1483. }
  1484. module_init(pch_spi_init);
  1485. static void __exit pch_spi_exit(void)
  1486. {
  1487. pci_unregister_driver(&pch_spi_pcidev_driver);
  1488. platform_driver_unregister(&pch_spi_pd_driver);
  1489. }
  1490. module_exit(pch_spi_exit);
  1491. module_param(use_dma, int, 0644);
  1492. MODULE_PARM_DESC(use_dma,
  1493. "to use DMA for data transfers pass 1 else 0; default 1");
  1494. MODULE_LICENSE("GPL");
  1495. MODULE_DESCRIPTION("Intel EG20T PCH/LAPIS Semiconductor ML7xxx IOH SPI Driver");
  1496. MODULE_DEVICE_TABLE(pci, pch_spi_pcidev_id);