Skip to content

Commit 8e02f85

Browse files
oakadPierre Ossman
authored andcommitted
tifm_sd: restructure initialization, removal and command handling
In order to support correct suspend and resume several changes were needed: 1. Switch from work_struct to tasklet for command handling. When device suspend is called workqueues are already frozen and can not be used. 2. Separate host initialization code from driver's probe and don't rely on interrupts for host initialization. This, in turn, addresses two problems: a) Resume needs to re-initialize the host, but can not assume that device interrupts were already re-armed. b) Previously, probe will return successfully before really knowing the state of the host, as host interrupts were not armed in time. Now it uses polling to determine the real host state before returning. 3. Separate termination code from driver's remove. Termination may be caused by resume, if media changed type or became unavailable during suspend. Signed-off-by: Alex Dubov <oakad@yahoo.com> Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
1 parent 83d420b commit 8e02f85

4 files changed

Lines changed: 116 additions & 108 deletions

File tree

‎drivers/misc/tifm_7xx1.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ static void tifm_7xx1_insert_media(struct work_struct *work)
201201
fm->max_sockets == 2);
202202
if (media_id) {
203203
ok_to_register = 0;
204-
new_sock = tifm_alloc_device(fm, cnt);
204+
new_sock = tifm_alloc_device(fm);
205205
if (new_sock) {
206206
new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
207207
cnt);
208208
new_sock->media_id = media_id;
209+
new_sock->socket_id = cnt;
209210
switch (media_id) {
210211
case 1:
211212
card_name = "xd";

‎drivers/misc/tifm_core.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,17 @@ EXPORT_SYMBOL(tifm_remove_adapter);
141141
void tifm_free_device(struct device *dev)
142142
{
143143
struct tifm_dev *fm_dev = container_of(dev, struct tifm_dev, dev);
144-
if (fm_dev->wq)
145-
destroy_workqueue(fm_dev->wq);
146144
kfree(fm_dev);
147145
}
148146
EXPORT_SYMBOL(tifm_free_device);
149147

150-
struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id)
148+
struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm)
151149
{
152150
struct tifm_dev *dev = kzalloc(sizeof(struct tifm_dev), GFP_KERNEL);
153151

154152
if (dev) {
155153
spin_lock_init(&dev->lock);
156-
snprintf(dev->wq_name, KOBJ_NAME_LEN, "tifm%u:%u", fm->id, id);
157-
dev->wq = create_singlethread_workqueue(dev->wq_name);
158-
if (!dev->wq) {
159-
kfree(dev);
160-
return NULL;
161-
}
154+
162155
dev->dev.parent = fm->dev;
163156
dev->dev.bus = &tifm_bus_type;
164157
dev->dev.release = tifm_free_device;

‎drivers/mmc/tifm_sd.c‎

Lines changed: 110 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ typedef enum {
7979

8080
enum {
8181
FIFO_RDY = 0x0001, /* hardware dependent value */
82-
HOST_REG = 0x0002,
8382
EJECT = 0x0004,
8483
EJECT_DONE = 0x0008,
8584
CARD_BUSY = 0x0010,
@@ -97,10 +96,10 @@ struct tifm_sd {
9796
unsigned int clk_div;
9897
unsigned long timeout_jiffies;
9998

99+
struct tasklet_struct finish_tasklet;
100100
struct timer_list timer;
101101
struct mmc_request *req;
102-
struct work_struct cmd_handler;
103-
wait_queue_head_t can_eject;
102+
wait_queue_head_t notify;
104103

105104
size_t written_blocks;
106105
size_t buffer_size;
@@ -317,7 +316,7 @@ static void tifm_sd_process_cmd(struct tifm_dev *sock, struct tifm_sd *host,
317316
}
318317
break;
319318
case READY:
320-
queue_work(sock->wq, &host->cmd_handler);
319+
tasklet_schedule(&host->finish_tasklet);
321320
return;
322321
}
323322

@@ -345,8 +344,6 @@ static unsigned int tifm_sd_signal_irq(struct tifm_dev *sock,
345344
host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
346345
writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
347346

348-
if (!(host->flags & HOST_REG))
349-
queue_work(sock->wq, &host->cmd_handler);
350347
if (!host->req)
351348
goto done;
352349

@@ -517,9 +514,9 @@ static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
517514
mmc_request_done(mmc, mrq);
518515
}
519516

520-
static void tifm_sd_end_cmd(struct work_struct *work)
517+
static void tifm_sd_end_cmd(unsigned long data)
521518
{
522-
struct tifm_sd *host = container_of(work, struct tifm_sd, cmd_handler);
519+
struct tifm_sd *host = (struct tifm_sd*)data;
523520
struct tifm_dev *sock = host->dev;
524521
struct mmc_host *mmc = tifm_get_drvdata(sock);
525522
struct mmc_request *mrq;
@@ -616,9 +613,9 @@ static void tifm_sd_request_nodma(struct mmc_host *mmc, struct mmc_request *mrq)
616613
mmc_request_done(mmc, mrq);
617614
}
618615

619-
static void tifm_sd_end_cmd_nodma(struct work_struct *work)
616+
static void tifm_sd_end_cmd_nodma(unsigned long data)
620617
{
621-
struct tifm_sd *host = container_of(work, struct tifm_sd, cmd_handler);
618+
struct tifm_sd *host = (struct tifm_sd*)data;
622619
struct tifm_dev *sock = host->dev;
623620
struct mmc_host *mmc = tifm_get_drvdata(sock);
624621
struct mmc_request *mrq;
@@ -666,11 +663,33 @@ static void tifm_sd_end_cmd_nodma(struct work_struct *work)
666663
mmc_request_done(mmc, mrq);
667664
}
668665

666+
static void tifm_sd_terminate(struct tifm_sd *host)
667+
{
668+
struct tifm_dev *sock = host->dev;
669+
unsigned long flags;
670+
671+
writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
672+
mmiowb();
673+
spin_lock_irqsave(&sock->lock, flags);
674+
host->flags |= EJECT;
675+
if (host->req) {
676+
writel(TIFM_FIFO_INT_SETALL,
677+
sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
678+
writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
679+
tasklet_schedule(&host->finish_tasklet);
680+
}
681+
spin_unlock_irqrestore(&sock->lock, flags);
682+
}
683+
669684
static void tifm_sd_abort(unsigned long data)
670685
{
686+
struct tifm_sd *host = (struct tifm_sd*)data;
687+
671688
printk(KERN_ERR DRIVER_NAME
672689
": card failed to respond for a long period of time");
673-
tifm_eject(((struct tifm_sd*)data)->dev);
690+
691+
tifm_sd_terminate(host);
692+
tifm_eject(host->dev);
674693
}
675694

676695
static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
@@ -739,7 +758,7 @@ static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
739758
// allow removal.
740759
if ((host->flags & EJECT) && ios->power_mode == MMC_POWER_OFF) {
741760
host->flags |= EJECT_DONE;
742-
wake_up_all(&host->can_eject);
761+
wake_up_all(&host->notify);
743762
}
744763

745764
spin_unlock_irqrestore(&sock->lock, flags);
@@ -767,21 +786,67 @@ static struct mmc_host_ops tifm_sd_ops = {
767786
.get_ro = tifm_sd_ro
768787
};
769788

770-
static void tifm_sd_register_host(struct work_struct *work)
789+
static int tifm_sd_initialize_host(struct tifm_sd *host)
771790
{
772-
struct tifm_sd *host = container_of(work, struct tifm_sd, cmd_handler);
791+
int rc;
792+
unsigned int host_status = 0;
773793
struct tifm_dev *sock = host->dev;
774-
struct mmc_host *mmc = tifm_get_drvdata(sock);
775-
unsigned long flags;
776794

777-
spin_lock_irqsave(&sock->lock, flags);
778-
del_timer(&host->timer);
779-
host->flags |= HOST_REG;
780-
PREPARE_WORK(&host->cmd_handler,
781-
no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd);
782-
spin_unlock_irqrestore(&sock->lock, flags);
783-
dev_dbg(&sock->dev, "adding host\n");
784-
mmc_add_host(mmc);
795+
writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
796+
mmiowb();
797+
host->clk_div = 61;
798+
host->clk_freq = 20000000;
799+
writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
800+
writel(host->clk_div | TIFM_MMCSD_POWER,
801+
sock->addr + SOCK_MMCSD_CONFIG);
802+
803+
/* wait up to 0.51 sec for reset */
804+
for (rc = 2; rc <= 256; rc <<= 1) {
805+
if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
806+
rc = 0;
807+
break;
808+
}
809+
msleep(rc);
810+
}
811+
812+
if (rc) {
813+
printk(KERN_ERR DRIVER_NAME
814+
": controller failed to reset\n");
815+
return -ENODEV;
816+
}
817+
818+
writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
819+
writel(host->clk_div | TIFM_MMCSD_POWER,
820+
sock->addr + SOCK_MMCSD_CONFIG);
821+
writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
822+
823+
// command timeout fixed to 64 clocks for now
824+
writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO);
825+
writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
826+
827+
/* INAB should take much less than reset */
828+
for (rc = 1; rc <= 16; rc <<= 1) {
829+
host_status = readl(sock->addr + SOCK_MMCSD_STATUS);
830+
writel(host_status, sock->addr + SOCK_MMCSD_STATUS);
831+
if (!(host_status & TIFM_MMCSD_ERRMASK)
832+
&& (host_status & TIFM_MMCSD_EOC)) {
833+
rc = 0;
834+
break;
835+
}
836+
msleep(rc);
837+
}
838+
839+
if (rc) {
840+
printk(KERN_ERR DRIVER_NAME
841+
": card not ready - probe failed on initialization\n");
842+
return -ENODEV;
843+
}
844+
845+
writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
846+
sock->addr + SOCK_MMCSD_INT_ENABLE);
847+
mmiowb();
848+
849+
return 0;
785850
}
786851

787852
static int tifm_sd_probe(struct tifm_dev *sock)
@@ -801,105 +866,55 @@ static int tifm_sd_probe(struct tifm_dev *sock)
801866
return -ENOMEM;
802867

803868
host = mmc_priv(mmc);
804-
host->dev = sock;
805-
host->clk_div = 61;
806-
init_waitqueue_head(&host->can_eject);
807-
INIT_WORK(&host->cmd_handler, tifm_sd_register_host);
808-
setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
809-
810869
tifm_set_drvdata(sock, mmc);
811-
sock->signal_irq = tifm_sd_signal_irq;
812-
813-
host->clk_freq = 20000000;
870+
host->dev = sock;
814871
host->timeout_jiffies = msecs_to_jiffies(1000);
815872

873+
init_waitqueue_head(&host->notify);
874+
tasklet_init(&host->finish_tasklet,
875+
no_dma ? tifm_sd_end_cmd_nodma : tifm_sd_end_cmd,
876+
(unsigned long)host);
877+
setup_timer(&host->timer, tifm_sd_abort, (unsigned long)host);
878+
816879
tifm_sd_ops.request = no_dma ? tifm_sd_request_nodma : tifm_sd_request;
817880
mmc->ops = &tifm_sd_ops;
818881
mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
819-
mmc->caps = MMC_CAP_4_BIT_DATA;
882+
mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
820883
mmc->f_min = 20000000 / 60;
821884
mmc->f_max = 24000000;
822885
mmc->max_hw_segs = 1;
823886
mmc->max_phys_segs = 1;
824887
mmc->max_sectors = 127;
825888
mmc->max_seg_size = mmc->max_sectors << 11; //2k maximum hw block length
889+
sock->signal_irq = tifm_sd_signal_irq;
890+
rc = tifm_sd_initialize_host(host);
826891

827-
writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
828-
writel(TIFM_MMCSD_RESET, sock->addr + SOCK_MMCSD_SYSTEM_CONTROL);
829-
writel(host->clk_div | TIFM_MMCSD_POWER,
830-
sock->addr + SOCK_MMCSD_CONFIG);
831-
832-
for (rc = 0; rc < 50; rc++) {
833-
/* Wait for reset ack */
834-
if (1 & readl(sock->addr + SOCK_MMCSD_SYSTEM_STATUS)) {
835-
rc = 0;
836-
break;
837-
}
838-
msleep(10);
839-
}
840-
841-
if (rc) {
842-
printk(KERN_ERR DRIVER_NAME
843-
": card not ready - probe failed\n");
844-
mmc_free_host(mmc);
845-
return -ENODEV;
846-
}
847-
848-
writel(0, sock->addr + SOCK_MMCSD_NUM_BLOCKS);
849-
writel(host->clk_div | TIFM_MMCSD_POWER,
850-
sock->addr + SOCK_MMCSD_CONFIG);
851-
writel(TIFM_MMCSD_RXDE, sock->addr + SOCK_MMCSD_BUFFER_CONFIG);
852-
writel(TIFM_MMCSD_DATAMASK | TIFM_MMCSD_ERRMASK,
853-
sock->addr + SOCK_MMCSD_INT_ENABLE);
854-
855-
writel(64, sock->addr + SOCK_MMCSD_COMMAND_TO); // command timeout 64 clocks for now
856-
writel(TIFM_MMCSD_INAB, sock->addr + SOCK_MMCSD_COMMAND);
857-
writel(host->clk_div | TIFM_MMCSD_POWER,
858-
sock->addr + SOCK_MMCSD_CONFIG);
859-
860-
mod_timer(&host->timer, jiffies + host->timeout_jiffies);
892+
if (!rc)
893+
rc = mmc_add_host(mmc);
894+
if (rc)
895+
goto out_free_mmc;
861896

862897
return 0;
863-
}
864-
865-
static int tifm_sd_host_is_down(struct tifm_dev *sock)
866-
{
867-
struct mmc_host *mmc = tifm_get_drvdata(sock);
868-
struct tifm_sd *host = mmc_priv(mmc);
869-
unsigned long flags;
870-
int rc = 0;
871-
872-
spin_lock_irqsave(&sock->lock, flags);
873-
rc = (host->flags & EJECT_DONE);
874-
spin_unlock_irqrestore(&sock->lock, flags);
898+
out_free_mmc:
899+
mmc_free_host(mmc);
875900
return rc;
876901
}
877902

878903
static void tifm_sd_remove(struct tifm_dev *sock)
879904
{
880905
struct mmc_host *mmc = tifm_get_drvdata(sock);
881906
struct tifm_sd *host = mmc_priv(mmc);
882-
unsigned long flags;
883907

884908
del_timer_sync(&host->timer);
885-
spin_lock_irqsave(&sock->lock, flags);
886-
host->flags |= EJECT;
887-
if (host->req)
888-
queue_work(sock->wq, &host->cmd_handler);
889-
spin_unlock_irqrestore(&sock->lock, flags);
890-
wait_event_timeout(host->can_eject, tifm_sd_host_is_down(sock),
891-
host->timeout_jiffies);
892-
893-
if (host->flags & HOST_REG)
894-
mmc_remove_host(mmc);
909+
tifm_sd_terminate(host);
910+
wait_event_timeout(host->notify, host->flags & EJECT_DONE,
911+
host->timeout_jiffies);
912+
tasklet_kill(&host->finish_tasklet);
913+
mmc_remove_host(mmc);
895914

896915
/* The meaning of the bit majority in this constant is unknown. */
897916
writel(0xfff8 & readl(sock->addr + SOCK_CONTROL),
898917
sock->addr + SOCK_CONTROL);
899-
writel(0, sock->addr + SOCK_MMCSD_INT_ENABLE);
900-
writel(TIFM_FIFO_INT_SETALL,
901-
sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
902-
writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
903918

904919
tifm_set_drvdata(sock, NULL);
905920
mmc_free_host(mmc);

‎include/linux/tifm.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ struct tifm_dev {
8989
char __iomem *addr;
9090
spinlock_t lock;
9191
tifm_media_id media_id;
92-
char wq_name[KOBJ_NAME_LEN];
93-
struct workqueue_struct *wq;
92+
unsigned int socket_id;
9493

9594
unsigned int (*signal_irq)(struct tifm_dev *sock,
9695
unsigned int sock_irq_status);
@@ -132,7 +131,7 @@ void tifm_free_device(struct device *dev);
132131
void tifm_free_adapter(struct tifm_adapter *fm);
133132
int tifm_add_adapter(struct tifm_adapter *fm);
134133
void tifm_remove_adapter(struct tifm_adapter *fm);
135-
struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm, unsigned int id);
134+
struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm);
136135
int tifm_register_driver(struct tifm_driver *drv);
137136
void tifm_unregister_driver(struct tifm_driver *drv);
138137
void tifm_eject(struct tifm_dev *sock);

0 commit comments

Comments
 (0)