forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/drivers/net/wireless/rockchip_wlan/cywdhd/bcmdhd/bcmsdspi_linux.c
....@@ -1,15 +1,16 @@
1
-/* SPDX-License-Identifier: GPL-2.0 */
21 /*
32 * Broadcom SPI Host Controller Driver - Linux Per-port
43 *
5
- * Copyright (C) 1999-2019, Broadcom Corporation
6
- *
4
+ * Portions of this code are copyright (c) 2022 Cypress Semiconductor Corporation
5
+ *
6
+ * Copyright (C) 1999-2017, Broadcom Corporation
7
+ *
78 * Unless you and Broadcom execute a separate written software license
89 * agreement governing use of this software, this software is licensed to you
910 * under the terms of the GNU General Public License version 2 (the "GPL"),
1011 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
1112 * following added to such license:
12
- *
13
+ *
1314 * As a special exception, the copyright holders of this software give you
1415 * permission to link this software with independent modules, and to copy and
1516 * distribute the resulting executable under terms of your choice, provided that
....@@ -17,7 +18,7 @@
1718 * the license of that module. An independent module is a module which is not
1819 * derived from this software. The special exception does not apply to any
1920 * modifications of the software.
20
- *
21
+ *
2122 * Notwithstanding the above, under no circumstances may you combine this
2223 * software in any way with any other Broadcom software provided under a license
2324 * other than the GPL, without Broadcom's express prior written consent.
....@@ -34,37 +35,257 @@
3435 #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
3536 #include <sdiovar.h> /* to get msglevel bit values */
3637
38
+#ifdef BCMSPI_ANDROID
39
+#include <bcmsdh.h>
40
+#include <bcmspibrcm.h>
41
+#include <linux/spi/spi.h>
42
+#else
3743 #include <pcicfg.h>
3844 #include <sdio.h> /* SDIO Device and Protocol Specs */
3945 #include <linux/sched.h> /* request_irq(), free_irq() */
4046 #include <bcmsdspi.h>
47
+#include <bcmdevs.h>
4148 #include <bcmspi.h>
49
+#endif /* BCMSPI_ANDROID */
4250
51
+#ifndef GSPIBCM
52
+#ifndef BCMSPI_ANDROID
4353 extern uint sd_crc;
4454 module_param(sd_crc, uint, 0);
4555
4656 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
4757 #define KERNEL26
48
-#endif
58
+#endif // endif
59
+#endif /* !BCMSPI_ANDROID */
4960
5061 struct sdos_info {
5162 sdioh_info_t *sd;
5263 spinlock_t lock;
64
+#ifndef BCMSPI_ANDROID
5365 wait_queue_head_t intr_wait_queue;
66
+#endif /* !BCMSPI_ANDROID */
5467 };
68
+#endif /* !GSPIBCM */
5569
70
+#ifndef BCMSPI_ANDROID
5671 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
5772 #define BLOCKABLE() (!in_atomic())
5873 #else
5974 #define BLOCKABLE() (!in_interrupt())
60
-#endif
75
+#endif // endif
6176
77
+/* For Broadcom PCI-SPI Host controller (Raggedstone) */
78
+#if defined(BCMSPI) && (defined(BCMPCISPIHOST) || defined(GSPIBCM))
79
+#ifndef SDLX_MSG
80
+#define SDLX_MSG(x) printf x
81
+#endif // endif
82
+extern void* bcmsdh_probe(osl_t *osh, void *dev, void *sdioh, void *adapter_info,
83
+ uint bus_type, uint bus_num, uint slot_num);
84
+extern int bcmsdh_remove(bcmsdh_info_t *bcmsdh);
85
+extern sdioh_info_t * sdioh_attach(osl_t *osh, void *bar0, uint irq);
86
+extern SDIOH_API_RC sdioh_detach(osl_t *osh, sdioh_info_t *sd);
87
+
88
+/* forward declarations for PCI probe and remove functions. */
89
+static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
90
+static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
91
+
92
+/* forward declarations for PCI probe and remove functions. */
93
+static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
94
+static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
95
+
96
+/**
97
+ * pci id table
98
+ */
99
+static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
100
+ { vendor: PCI_ANY_ID,
101
+ device: PCI_ANY_ID,
102
+ subvendor: PCI_ANY_ID,
103
+ subdevice: PCI_ANY_ID,
104
+ class: 0,
105
+ class_mask: 0,
106
+ driver_data: 0,
107
+ },
108
+ { 0, }
109
+};
110
+MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
111
+
112
+/**
113
+ * PCI-SPI Host Controller: pci driver info
114
+ */
115
+static struct pci_driver bcmsdh_pci_driver = {
116
+ node: {},
117
+ name: "bcmsdh",
118
+ id_table: bcmsdh_pci_devid,
119
+ probe: bcmsdh_pci_probe,
120
+ remove: bcmsdh_pci_remove,
121
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
122
+ save_state: NULL,
123
+#endif // endif
124
+ suspend: NULL,
125
+ resume: NULL,
126
+};
127
+
128
+/* Force detection to a particular PCI */
129
+/* slot only . Allows for having multiple */
130
+/* WL devices at once in a PC */
131
+/* Only one instance of dhd will be */
132
+/* usable at a time */
133
+/* Upper word is bus number, */
134
+/* lower word is slot number */
135
+/* Default value of 0xffffffff turns this off */
136
+extern uint sd_pci_slot;
137
+module_param(sd_pci_slot, uint, 0);
138
+
139
+/**
140
+ * Detect supported Host Controller and attach if found.
141
+ *
142
+ * Determine if the device described by pdev is a supported PCI Host
143
+ * Controller. If so, attach to it and attach to the target device.
144
+ */
145
+static int __devinit
146
+bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
147
+{
148
+ osl_t *osh = NULL;
149
+ sdioh_info_t *sdioh = NULL;
150
+ void *bcmsdh;
151
+
152
+ int rc;
153
+
154
+ if (sd_pci_slot != 0xFFFFFFFF) {
155
+ if (pdev->bus->number != (sd_pci_slot>>16) ||
156
+ PCI_SLOT(pdev->devfn) != (sd_pci_slot&0xffff)) {
157
+ SDLX_MSG(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
158
+ __FUNCTION__,
159
+ bcmsdh_chipmatch(pdev->vendor, pdev->device)
160
+ ?"Found compatible SDIOHC"
161
+ :"Probing unknown device",
162
+ pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
163
+ pdev->device));
164
+ return -ENODEV;
165
+ }
166
+ SDLX_MSG(("%s: %s: bus %X, slot %X, vendor %X, device %X (good PCI location)\n",
167
+ __FUNCTION__,
168
+ bcmsdh_chipmatch(pdev->vendor, pdev->device)
169
+ ?"Using compatible SDIOHC"
170
+ :"WARNING, forced use of unkown device",
171
+ pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor, pdev->device));
172
+ }
173
+
174
+ if ((pdev->vendor == VENDOR_TI) && ((pdev->device == PCIXX21_FLASHMEDIA_ID) ||
175
+ (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
176
+ uint32 config_reg;
177
+
178
+ SDLX_MSG(("%s: Disabling TI FlashMedia Controller.\n", __FUNCTION__));
179
+ if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
180
+ SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
181
+ goto err;
182
+ }
183
+ config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
184
+
185
+ /*
186
+ * Set MMC_SD_DIS bit in FlashMedia Controller.
187
+ * Disbling the SD/MMC Controller in the FlashMedia Controller
188
+ * allows the Standard SD Host Controller to take over control
189
+ * of the SD Slot.
190
+ */
191
+ config_reg |= 0x02;
192
+ OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
193
+ osl_detach(osh);
194
+ }
195
+ /* match this pci device with what we support */
196
+ /* we can't solely rely on this to believe it is our SDIO Host Controller! */
197
+ if (!bcmsdh_chipmatch(pdev->vendor, pdev->device)) {
198
+ if (pdev->vendor == VENDOR_BROADCOM) {
199
+ SDLX_MSG(("%s: Unknown Broadcom device (vendor: %#x, device: %#x).\n",
200
+ __FUNCTION__, pdev->vendor, pdev->device));
201
+ }
202
+ return -ENODEV;
203
+ }
204
+
205
+ /* this is a pci device we might support */
206
+ SDLX_MSG(("%s: Found possible SDIO Host Controller: bus %d slot %d func %d irq %d\n",
207
+ __FUNCTION__,
208
+ pdev->bus->number, PCI_SLOT(pdev->devfn),
209
+ PCI_FUNC(pdev->devfn), pdev->irq));
210
+
211
+ /* use bcmsdh_query_device() to get the vendor ID of the target device so
212
+ * it will eventually appear in the Broadcom string on the console
213
+ */
214
+
215
+ /* allocate SDIO Host Controller state info */
216
+ if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
217
+ SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
218
+ goto err;
219
+ }
220
+
221
+ /* map to address where host can access */
222
+ pci_set_master(pdev);
223
+ rc = pci_enable_device(pdev);
224
+ if (rc) {
225
+ SDLX_MSG(("%s: Cannot enable PCI device\n", __FUNCTION__));
226
+ goto err;
227
+ }
228
+
229
+ if (!(sdioh = sdioh_attach(osh, (void *)(ulong)pci_resource_start(pdev, 0), pdev->irq))) {
230
+ SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
231
+ goto err;
232
+ }
233
+
234
+#ifdef GSPIBCM
235
+ bcmsdh = bcmsdh_probe(osh, &pdev->dev, sdioh, NULL, PCI_BUS, -1, -1);
236
+ if (bcmsdh == NULL) {
237
+#else
238
+ sdioh->bcmsdh = bcmsdh_probe(osh, &pdev->dev, sdioh, NULL, PCI_BUS, -1, -1);
239
+ if (sdioh->bcmsdh == NULL) {
240
+#endif /* GSPIBCM */
241
+ sd_err(("%s: bcmsdh_probe failed\n", __FUNCTION__));
242
+ goto err;
243
+ }
244
+
245
+ pci_set_drvdata(pdev, sdioh);
246
+ return 0;
247
+
248
+ /* error handling */
249
+err:
250
+ if (sdioh != NULL)
251
+ sdioh_detach(osh, sdioh);
252
+ if (osh != NULL)
253
+ osl_detach(osh);
254
+ return -ENOMEM;
255
+
256
+}
257
+
258
+/**
259
+ * Detach from target devices and PCI-SPI Host Controller
260
+ */
261
+static void __devexit
262
+bcmsdh_pci_remove(struct pci_dev *pdev)
263
+{
264
+
265
+ sdioh_info_t *sdioh = NULL;
266
+ osl_t *osh;
267
+
268
+ sdioh = pci_get_drvdata(pdev);
269
+ if (sdioh == NULL) {
270
+ sd_err(("%s: error, no sdh handler found\n", __FUNCTION__));
271
+ return;
272
+ }
273
+
274
+ osh = sdioh->osh;
275
+ bcmsdh_remove(sdioh->bcmsdh);
276
+ sdioh_detach(osh, sdioh);
277
+ osl_detach(osh);
278
+
279
+}
280
+#endif /* BCMSPI && BCMPCISPIHOST */
281
+
282
+#ifndef GSPIBCM
62283 /* Interrupt handler */
63284 static irqreturn_t
64285 sdspi_isr(int irq, void *dev_id
65286 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
66287 , struct pt_regs *ptregs
67
-#endif
288
+#endif // endif
68289 )
69290 {
70291 sdioh_info_t *sd;
....@@ -89,17 +310,100 @@
89310 return IRQ_RETVAL(ours);
90311 }
91312 }
313
+#endif /* !GSPIBCM */
314
+#endif /* !BCMSPI_ANDROID */
92315
316
+#ifdef BCMSPI_ANDROID
317
+static struct spi_device *gBCMSPI = NULL;
93318
319
+extern int bcmsdh_probe(struct device *dev);
320
+extern int bcmsdh_remove(struct device *dev);
321
+
322
+static int bcmsdh_spi_probe(struct spi_device *spi_dev)
323
+{
324
+ int ret = 0;
325
+
326
+ gBCMSPI = spi_dev;
327
+
328
+#ifdef SPI_PIO_32BIT_RW
329
+ spi_dev->bits_per_word = 32;
330
+#else
331
+ spi_dev->bits_per_word = 8;
332
+#endif /* SPI_PIO_32BIT_RW */
333
+ ret = spi_setup(spi_dev);
334
+
335
+ if (ret) {
336
+ sd_err(("bcmsdh_spi_probe: spi_setup fail with %d\n", ret));
337
+ }
338
+ sd_err(("bcmsdh_spi_probe: spi_setup with %d, bits_per_word=%d\n",
339
+ ret, spi_dev->bits_per_word));
340
+ ret = bcmsdh_probe(&spi_dev->dev);
341
+
342
+ return ret;
343
+}
344
+
345
+static int bcmsdh_spi_remove(struct spi_device *spi_dev)
346
+{
347
+ int ret = 0;
348
+
349
+ ret = bcmsdh_remove(&spi_dev->dev);
350
+ gBCMSPI = NULL;
351
+
352
+ return ret;
353
+}
354
+
355
+static struct spi_driver bcmsdh_spi_driver = {
356
+ .probe = bcmsdh_spi_probe,
357
+ .remove = bcmsdh_spi_remove,
358
+ .driver = {
359
+ .name = "wlan_spi",
360
+ .bus = &spi_bus_type,
361
+ .owner = THIS_MODULE,
362
+ },
363
+};
364
+#endif /* BCMSPI_ANDROID */
365
+
366
+/*
367
+ * module init
368
+*/
369
+int bcmsdh_register_client_driver(void)
370
+{
371
+ int error = 0;
372
+ sd_trace(("bcmsdh_gspi: %s Enter\n", __FUNCTION__));
373
+#if defined(BCMSPI) && (defined(BCMPCISPIHOST) || defined(GSPIBCM))
374
+ error = pci_module_init(&bcmsdh_pci_driver);
375
+#else
376
+ error = spi_register_driver(&bcmsdh_spi_driver);
377
+#endif /* BCMSPI && BCMPCISPIHOST */
378
+
379
+ return error;
380
+}
381
+
382
+/*
383
+ * module cleanup
384
+*/
385
+void bcmsdh_unregister_client_driver(void)
386
+{
387
+ sd_trace(("%s Enter\n", __FUNCTION__));
388
+#if defined(BCMSPI) && (defined(BCMPCISPIHOST) || defined(GSPIBCM))
389
+ pci_unregister_driver(&bcmsdh_pci_driver);
390
+#else
391
+ spi_unregister_driver(&bcmsdh_spi_driver);
392
+#endif /* BCMSPI && BCMPCISPIHOST */
393
+}
394
+
395
+#ifndef GSPIBCM
94396 /* Register with Linux for interrupts */
95397 int
96398 spi_register_irq(sdioh_info_t *sd, uint irq)
97399 {
400
+#ifndef BCMSPI_ANDROID
98401 sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
99402 if (request_irq(irq, sdspi_isr, IRQF_SHARED, "bcmsdspi", sd) < 0) {
100403 sd_err(("%s: request_irq() failed\n", __FUNCTION__));
101404 return ERROR;
102405 }
406
+#endif /* !BCMSPI_ANDROID */
103407 return SUCCESS;
104408 }
105409
....@@ -107,10 +411,13 @@
107411 void
108412 spi_free_irq(uint irq, sdioh_info_t *sd)
109413 {
414
+#ifndef BCMSPI_ANDROID
110415 free_irq(irq, sd);
416
+#endif /* !BCMSPI_ANDROID */
111417 }
112418
113419 /* Map Host controller registers */
420
+#ifndef BCMSPI_ANDROID
114421 uint32 *
115422 spi_reg_map(osl_t *osh, uintptr addr, int size)
116423 {
....@@ -122,6 +429,7 @@
122429 {
123430 REG_UNMAP((void*)(uintptr)addr);
124431 }
432
+#endif /* !BCMSPI_ANDROID */
125433
126434 int
127435 spi_osinit(sdioh_info_t *sd)
....@@ -135,7 +443,9 @@
135443
136444 sdos->sd = sd;
137445 spin_lock_init(&sdos->lock);
446
+#ifndef BCMSPI_ANDROID
138447 init_waitqueue_head(&sdos->intr_wait_queue);
448
+#endif /* !BCMSPI_ANDROID */
139449 return BCME_OK;
140450 }
141451
....@@ -166,19 +476,23 @@
166476 return SDIOH_API_RC_FAIL;
167477 }
168478
479
+#ifndef BCMSPI_ANDROID
169480 if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
170481 sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
171482 return SDIOH_API_RC_FAIL;
172483 }
484
+#endif /* !BCMSPI_ANDROID */
173485
174486 /* Ensure atomicity for enable/disable calls */
175487 spin_lock_irqsave(&sdos->lock, flags);
176488
177489 sd->client_intr_enabled = enable;
490
+#ifndef BCMSPI_ANDROID
178491 if (enable && !sd->lockcount)
179492 spi_devintr_on(sd);
180493 else
181494 spi_devintr_off(sd);
495
+#endif /* !BCMSPI_ANDROID */
182496
183497 spin_unlock_irqrestore(&sdos->lock, flags);
184498
....@@ -202,7 +516,12 @@
202516 sd_err(("%s: Already locked!\n", __FUNCTION__));
203517 ASSERT(sd->lockcount == 0);
204518 }
519
+#ifdef BCMSPI_ANDROID
520
+ if (sd->client_intr_enabled)
521
+ bcmsdh_oob_intr_set(0);
522
+#else
205523 spi_devintr_off(sd);
524
+#endif /* BCMSPI_ANDROID */
206525 sd->lockcount++;
207526 spin_unlock_irqrestore(&sdos->lock, flags);
208527 }
....@@ -222,16 +541,21 @@
222541
223542 spin_lock_irqsave(&sdos->lock, flags);
224543 if (--sd->lockcount == 0 && sd->client_intr_enabled) {
544
+#ifdef BCMSPI_ANDROID
545
+ bcmsdh_oob_intr_set(1);
546
+#else
225547 spi_devintr_on(sd);
548
+#endif /* BCMSPI_ANDROID */
226549 }
227550 spin_unlock_irqrestore(&sdos->lock, flags);
228551 }
229552
553
+#ifndef BCMSPI_ANDROID
230554 void spi_waitbits(sdioh_info_t *sd, bool yield)
231555 {
232556 #ifndef BCMSDYIELD
233557 ASSERT(!yield);
234
-#endif
558
+#endif // endif
235559 sd_trace(("%s: yield %d canblock %d\n",
236560 __FUNCTION__, yield, BLOCKABLE()));
237561
....@@ -251,3 +575,84 @@
251575 }
252576
253577 }
578
+#else /* !BCMSPI_ANDROID */
579
+int bcmgspi_dump = 0; /* Set to dump complete trace of all SPI bus transactions */
580
+
581
+static void
582
+hexdump(char *pfx, unsigned char *msg, int msglen)
583
+{
584
+ int i, col;
585
+ char buf[80];
586
+
587
+ ASSERT(strlen(pfx) + 49 <= sizeof(buf));
588
+
589
+ col = 0;
590
+
591
+ for (i = 0; i < msglen; i++, col++) {
592
+ if (col % 16 == 0)
593
+ strcpy(buf, pfx);
594
+ sprintf(buf + strlen(buf), "%02x", msg[i]);
595
+ if ((col + 1) % 16 == 0)
596
+ printf("%s\n", buf);
597
+ else
598
+ sprintf(buf + strlen(buf), " ");
599
+ }
600
+
601
+ if (col % 16 != 0)
602
+ printf("%s\n", buf);
603
+}
604
+
605
+/* Send/Receive an SPI Packet */
606
+void
607
+spi_sendrecv(sdioh_info_t *sd, uint8 *msg_out, uint8 *msg_in, int msglen)
608
+{
609
+ int write = 0;
610
+ int tx_len = 0;
611
+ struct spi_message msg;
612
+ struct spi_transfer t[2];
613
+
614
+ spi_message_init(&msg);
615
+ memset(t, 0, 2*sizeof(struct spi_transfer));
616
+
617
+ if (sd->wordlen == 2)
618
+#if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
619
+ write = msg_out[2] & 0x80;
620
+#else
621
+ write = msg_out[1] & 0x80;
622
+#endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
623
+ if (sd->wordlen == 4)
624
+#if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
625
+ write = msg_out[0] & 0x80;
626
+#else
627
+ write = msg_out[3] & 0x80;
628
+#endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
629
+
630
+ if (bcmgspi_dump) {
631
+ hexdump(" OUT: ", msg_out, msglen);
632
+ }
633
+
634
+ tx_len = write ? msglen-4 : 4;
635
+
636
+ sd_trace(("spi_sendrecv: %s, wordlen %d, cmd : 0x%02x 0x%02x 0x%02x 0x%02x\n",
637
+ write ? "WR" : "RD", sd->wordlen,
638
+ msg_out[0], msg_out[1], msg_out[2], msg_out[3]));
639
+
640
+ t[0].tx_buf = (char *)&msg_out[0];
641
+ t[0].rx_buf = 0;
642
+ t[0].len = tx_len;
643
+
644
+ spi_message_add_tail(&t[0], &msg);
645
+
646
+ t[1].rx_buf = (char *)&msg_in[tx_len];
647
+ t[1].tx_buf = 0;
648
+ t[1].len = msglen-tx_len;
649
+
650
+ spi_message_add_tail(&t[1], &msg);
651
+ spi_sync(gBCMSPI, &msg);
652
+
653
+ if (bcmgspi_dump) {
654
+ hexdump(" IN : ", msg_in, msglen);
655
+ }
656
+}
657
+#endif /* !BCMSPI_ANDROID */
658
+#endif /* !GSPIBCM */