1 // SPDX-License-Identifier: GPL-2.0-only
3 * tifm_7xx1.c - TI FlashMedia driver
5 * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
8 #include <linux/tifm.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/module.h>
12 #define DRIVER_NAME "tifm_7xx1"
13 #define DRIVER_VERSION "0.8"
15 #define TIFM_IRQ_ENABLE 0x80000000
16 #define TIFM_IRQ_SOCKMASK(x) (x)
17 #define TIFM_IRQ_CARDMASK(x) ((x) << 8)
18 #define TIFM_IRQ_FIFOMASK(x) ((x) << 16)
19 #define TIFM_IRQ_SETALL 0xffffffff
21 static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
22 struct tifm_dev *sock)
26 static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
30 spin_lock_irqsave(&fm->lock, flags);
31 fm->socket_change_set |= 1 << sock->socket_id;
32 tifm_queue_work(&fm->media_switcher);
33 spin_unlock_irqrestore(&fm->lock, flags);
36 static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
38 struct tifm_adapter *fm = dev_id;
39 struct tifm_dev *sock;
40 unsigned int irq_status, cnt;
43 irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
44 if (irq_status == 0 || irq_status == (~0)) {
45 spin_unlock(&fm->lock);
49 if (irq_status & TIFM_IRQ_ENABLE) {
50 writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
52 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
53 sock = fm->sockets[cnt];
55 if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
56 sock->data_event(sock);
57 if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
58 sock->card_event(sock);
62 fm->socket_change_set |= irq_status
63 & ((1 << fm->num_sockets) - 1);
65 writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
68 complete_all(fm->finish_me);
69 else if (!fm->socket_change_set)
70 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
72 tifm_queue_work(&fm->media_switcher);
74 spin_unlock(&fm->lock);
78 static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
83 writel(0x0e00, sock_addr + SOCK_CONTROL);
85 for (cnt = 16; cnt <= 256; cnt <<= 1) {
86 if (!(TIFM_SOCK_STATE_POWERED
87 & readl(sock_addr + SOCK_PRESENT_STATE)))
93 s_state = readl(sock_addr + SOCK_PRESENT_STATE);
94 if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
97 writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
98 sock_addr + SOCK_CONTROL);
100 /* xd needs some extra time before power on */
101 if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7)
105 writel((s_state & TIFM_CTRL_POWER_MASK) | 0x0c00,
106 sock_addr + SOCK_CONTROL);
107 /* wait for power to stabilize */
109 for (cnt = 16; cnt <= 256; cnt <<= 1) {
110 if ((TIFM_SOCK_STATE_POWERED
111 & readl(sock_addr + SOCK_PRESENT_STATE)))
117 writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
118 sock_addr + SOCK_CONTROL);
120 return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
123 inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr)
125 writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL),
126 sock_addr + SOCK_CONTROL);
129 inline static char __iomem *
130 tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
132 return base_addr + ((sock_num + 1) << 10);
135 static void tifm_7xx1_switch_media(struct work_struct *work)
137 struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
139 struct tifm_dev *sock;
140 char __iomem *sock_addr;
142 unsigned char media_id;
143 unsigned int socket_change_set, cnt;
145 spin_lock_irqsave(&fm->lock, flags);
146 socket_change_set = fm->socket_change_set;
147 fm->socket_change_set = 0;
149 dev_dbg(fm->dev.parent, "checking media set %x\n",
152 if (!socket_change_set) {
153 spin_unlock_irqrestore(&fm->lock, flags);
157 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
158 if (!(socket_change_set & (1 << cnt)))
160 sock = fm->sockets[cnt];
163 "%s : demand removing card from socket %u:%u\n",
164 dev_name(&fm->dev), fm->id, cnt);
165 fm->sockets[cnt] = NULL;
166 sock_addr = sock->addr;
167 spin_unlock_irqrestore(&fm->lock, flags);
168 device_unregister(&sock->dev);
169 spin_lock_irqsave(&fm->lock, flags);
170 tifm_7xx1_sock_power_off(sock_addr);
171 writel(0x0e00, sock_addr + SOCK_CONTROL);
174 spin_unlock_irqrestore(&fm->lock, flags);
176 media_id = tifm_7xx1_toggle_sock_power(
177 tifm_7xx1_sock_addr(fm->addr, cnt));
179 // tifm_alloc_device will check if media_id is valid
180 sock = tifm_alloc_device(fm, cnt, media_id);
182 sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
184 if (!device_register(&sock->dev)) {
185 spin_lock_irqsave(&fm->lock, flags);
186 if (!fm->sockets[cnt]) {
187 fm->sockets[cnt] = sock;
190 spin_unlock_irqrestore(&fm->lock, flags);
193 put_device(&sock->dev);
195 spin_lock_irqsave(&fm->lock, flags);
198 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
199 | TIFM_IRQ_CARDMASK(socket_change_set),
200 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
202 writel(TIFM_IRQ_FIFOMASK(socket_change_set)
203 | TIFM_IRQ_CARDMASK(socket_change_set),
204 fm->addr + FM_SET_INTERRUPT_ENABLE);
206 writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
207 spin_unlock_irqrestore(&fm->lock, flags);
210 static int __maybe_unused tifm_7xx1_suspend(struct device *dev_d)
212 struct pci_dev *dev = to_pci_dev(dev_d);
213 struct tifm_adapter *fm = pci_get_drvdata(dev);
216 dev_dbg(&dev->dev, "suspending host\n");
218 for (cnt = 0; cnt < fm->num_sockets; cnt++) {
219 if (fm->sockets[cnt])
220 tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr);
223 device_wakeup_disable(dev_d);
227 static int __maybe_unused tifm_7xx1_resume(struct device *dev_d)
229 struct pci_dev *dev = to_pci_dev(dev_d);
230 struct tifm_adapter *fm = pci_get_drvdata(dev);
232 unsigned long timeout;
233 unsigned int good_sockets = 0, bad_sockets = 0;
235 /* Maximum number of entries is 4 */
236 unsigned char new_ids[4];
237 DECLARE_COMPLETION_ONSTACK(finish_resume);
239 if (WARN_ON(fm->num_sockets > ARRAY_SIZE(new_ids)))
244 dev_dbg(&dev->dev, "resuming host\n");
246 for (rc = 0; rc < fm->num_sockets; rc++)
247 new_ids[rc] = tifm_7xx1_toggle_sock_power(
248 tifm_7xx1_sock_addr(fm->addr, rc));
249 spin_lock_irqsave(&fm->lock, flags);
250 for (rc = 0; rc < fm->num_sockets; rc++) {
251 if (fm->sockets[rc]) {
252 if (fm->sockets[rc]->type == new_ids[rc])
253 good_sockets |= 1 << rc;
255 bad_sockets |= 1 << rc;
259 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
260 fm->addr + FM_SET_INTERRUPT_ENABLE);
261 dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
262 good_sockets, bad_sockets);
264 fm->socket_change_set = 0;
266 fm->finish_me = &finish_resume;
267 spin_unlock_irqrestore(&fm->lock, flags);
268 timeout = wait_for_completion_timeout(&finish_resume, HZ);
269 dev_dbg(&dev->dev, "wait returned %lu\n", timeout);
270 writel(TIFM_IRQ_FIFOMASK(good_sockets)
271 | TIFM_IRQ_CARDMASK(good_sockets),
272 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
273 writel(TIFM_IRQ_FIFOMASK(good_sockets)
274 | TIFM_IRQ_CARDMASK(good_sockets),
275 fm->addr + FM_SET_INTERRUPT_ENABLE);
276 spin_lock_irqsave(&fm->lock, flags);
277 fm->finish_me = NULL;
278 fm->socket_change_set ^= good_sockets & fm->socket_change_set;
281 fm->socket_change_set |= bad_sockets;
282 if (fm->socket_change_set)
283 tifm_queue_work(&fm->media_switcher);
285 spin_unlock_irqrestore(&fm->lock, flags);
286 writel(TIFM_IRQ_ENABLE,
287 fm->addr + FM_SET_INTERRUPT_ENABLE);
292 static int tifm_7xx1_dummy_has_ms_pif(struct tifm_adapter *fm,
293 struct tifm_dev *sock)
298 static int tifm_7xx1_has_ms_pif(struct tifm_adapter *fm, struct tifm_dev *sock)
300 if (((fm->num_sockets == 4) && (sock->socket_id == 2))
301 || ((fm->num_sockets == 2) && (sock->socket_id == 0)))
307 static int tifm_7xx1_probe(struct pci_dev *dev,
308 const struct pci_device_id *dev_id)
310 struct tifm_adapter *fm;
311 int pci_dev_busy = 0;
314 rc = dma_set_mask(&dev->dev, DMA_BIT_MASK(32));
318 rc = pci_enable_device(dev);
324 rc = pci_request_regions(dev, DRIVER_NAME);
332 fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
339 INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
340 fm->eject = tifm_7xx1_eject;
341 fm->has_ms_pif = tifm_7xx1_has_ms_pif;
342 pci_set_drvdata(dev, fm);
344 fm->addr = pci_ioremap_bar(dev, 0);
350 rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
354 rc = tifm_add_adapter(fm);
358 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
359 fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
360 writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
361 fm->addr + FM_SET_INTERRUPT_ENABLE);
365 free_irq(dev->irq, fm);
369 tifm_free_adapter(fm);
372 pci_release_regions(dev);
375 pci_disable_device(dev);
379 static void tifm_7xx1_remove(struct pci_dev *dev)
381 struct tifm_adapter *fm = pci_get_drvdata(dev);
384 fm->eject = tifm_7xx1_dummy_eject;
385 fm->has_ms_pif = tifm_7xx1_dummy_has_ms_pif;
386 writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
387 free_irq(dev->irq, fm);
389 tifm_remove_adapter(fm);
391 for (cnt = 0; cnt < fm->num_sockets; cnt++)
392 tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt));
396 pci_release_regions(dev);
398 pci_disable_device(dev);
399 tifm_free_adapter(fm);
402 static const struct pci_device_id tifm_7xx1_pci_tbl[] = {
403 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
404 PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
405 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
406 PCI_ANY_ID, 0, 0, 0 },
407 { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
408 PCI_ANY_ID, 0, 0, 0 },
412 static SIMPLE_DEV_PM_OPS(tifm_7xx1_pm_ops, tifm_7xx1_suspend, tifm_7xx1_resume);
414 static struct pci_driver tifm_7xx1_driver = {
416 .id_table = tifm_7xx1_pci_tbl,
417 .probe = tifm_7xx1_probe,
418 .remove = tifm_7xx1_remove,
419 .driver.pm = &tifm_7xx1_pm_ops,
422 module_pci_driver(tifm_7xx1_driver);
423 MODULE_AUTHOR("Alex Dubov");
424 MODULE_DESCRIPTION("TI FlashMedia host driver");
425 MODULE_LICENSE("GPL");
426 MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
427 MODULE_VERSION(DRIVER_VERSION);