2 * Wireless Host Controller (WHC) periodic schedule management.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/uwb/umc.h>
21 #include <linux/usb.h>
23 #include <linux/uwb/debug.h>
25 #include "../../wusbcore/wusbhc.h"
30 static void dump_pzl(struct whc *whc, const char *tag)
32 struct device *dev = &whc->umc->dev;
33 struct whc_qset *qset;
36 d_printf(4, dev, "PZL %s\n", tag);
38 for (period = 0; period < 5; period++) {
39 d_printf(4, dev, "Period %d\n", period);
40 list_for_each_entry(qset, &whc->periodic_list[period], list_node) {
46 static inline void dump_pzl(struct whc *whc, const char *tag)
51 static void update_pzl_pointers(struct whc *whc, int period, u64 addr)
55 whc_qset_set_link_ptr(&whc->pz_list[0], addr);
56 whc_qset_set_link_ptr(&whc->pz_list[2], addr);
57 whc_qset_set_link_ptr(&whc->pz_list[4], addr);
58 whc_qset_set_link_ptr(&whc->pz_list[6], addr);
59 whc_qset_set_link_ptr(&whc->pz_list[8], addr);
60 whc_qset_set_link_ptr(&whc->pz_list[10], addr);
61 whc_qset_set_link_ptr(&whc->pz_list[12], addr);
62 whc_qset_set_link_ptr(&whc->pz_list[14], addr);
65 whc_qset_set_link_ptr(&whc->pz_list[1], addr);
66 whc_qset_set_link_ptr(&whc->pz_list[5], addr);
67 whc_qset_set_link_ptr(&whc->pz_list[9], addr);
68 whc_qset_set_link_ptr(&whc->pz_list[13], addr);
71 whc_qset_set_link_ptr(&whc->pz_list[3], addr);
72 whc_qset_set_link_ptr(&whc->pz_list[11], addr);
75 whc_qset_set_link_ptr(&whc->pz_list[7], addr);
78 whc_qset_set_link_ptr(&whc->pz_list[15], addr);
84 * Return the 'period' to use for this qset. The minimum interval for
85 * the endpoint is used so whatever urbs are submitted the device is
86 * polled often enough.
88 static int qset_get_period(struct whc *whc, struct whc_qset *qset)
90 uint8_t bInterval = qset->ep->desc.bInterval;
99 static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
103 period = qset_get_period(whc, qset);
105 qset_clear(whc, qset);
106 list_move(&qset->list_node, &whc->periodic_list[period]);
107 qset->in_sw_list = true;
110 static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
112 list_move(&qset->list_node, &whc->periodic_removed_list);
113 qset->in_hw_list = false;
114 qset->in_sw_list = false;
118 * pzl_process_qset - process any recently inactivated or halted qTDs
121 * After inactive qTDs are removed, new qTDs can be added if the
122 * urb queue still contains URBs.
124 * Returns the schedule updates required.
126 static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
128 enum whc_update update = 0;
136 td = &qset->qtd[qset->td_start];
137 status = le32_to_cpu(td->status);
140 * Nothing to do with a still active qTD.
142 if (status & QTD_STS_ACTIVE)
145 if (status & QTD_STS_HALTED) {
147 process_halted_qtd(whc, qset, td);
151 /* Mmm, a completed qTD. */
152 process_inactive_qtd(whc, qset, td);
155 update |= qset_add_qtds(whc, qset);
159 * If there are no qTDs in this qset, remove it from the PZL.
161 if (qset->remove && qset->ntds == 0) {
162 pzl_qset_remove(whc, qset);
163 update |= WHC_UPDATE_REMOVED;
170 * pzl_start - start the periodic schedule
171 * @whc: the WHCI host controller
173 * The PZL must be valid (e.g., all entries in the list should have
176 void pzl_start(struct whc *whc)
178 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
180 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
181 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
182 WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
187 * pzl_stop - stop the periodic schedule
188 * @whc: the WHCI host controller
190 void pzl_stop(struct whc *whc)
192 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
193 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
194 WUSBSTS_PERIODIC_SCHED, 0,
198 void pzl_update(struct whc *whc, uint32_t wusbcmd)
200 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
201 wait_event(whc->periodic_list_wq,
202 (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0);
205 static void update_pzl_hw_view(struct whc *whc)
207 struct whc_qset *qset, *t;
211 for (period = 0; period < 5; period++) {
212 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
213 whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
214 tmp_qh = qset->qset_dma;
215 qset->in_hw_list = true;
217 update_pzl_pointers(whc, period, tmp_qh);
222 * scan_periodic_work - scan the PZL for qsets to process.
224 * Process each qset in the PZL in turn and then signal the WHC that
225 * the PZL has been updated.
227 * Then start, stop or update the periodic schedule as required.
229 void scan_periodic_work(struct work_struct *work)
231 struct whc *whc = container_of(work, struct whc, periodic_work);
232 struct whc_qset *qset, *t;
233 enum whc_update update = 0;
236 spin_lock_irq(&whc->lock);
238 dump_pzl(whc, "before processing");
240 for (period = 4; period >= 0; period--) {
241 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
242 if (!qset->in_hw_list)
243 update |= WHC_UPDATE_ADDED;
244 update |= pzl_process_qset(whc, qset);
248 if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
249 update_pzl_hw_view(whc);
251 dump_pzl(whc, "after processing");
253 spin_unlock_irq(&whc->lock);
256 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
257 if (update & WHC_UPDATE_REMOVED)
258 wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
259 pzl_update(whc, wusbcmd);
263 * Now that the PZL is updated, complete the removal of any
266 spin_lock(&whc->lock);
268 list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
269 qset_remove_complete(whc, qset);
272 spin_unlock(&whc->lock);
276 * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
277 * @whc: the WHCI host controller
278 * @urb: the URB to enqueue
279 * @mem_flags: flags for any memory allocations
281 * The qset for the endpoint is obtained and the urb queued on to it.
283 * Work is scheduled to update the hardware's view of the PZL.
285 int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
287 struct whc_qset *qset;
291 spin_lock_irqsave(&whc->lock, flags);
293 qset = get_qset(whc, urb, GFP_ATOMIC);
297 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
299 usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
300 if (!qset->in_sw_list)
301 qset_insert_in_sw_list(whc, qset);
304 spin_unlock_irqrestore(&whc->lock, flags);
307 queue_work(whc->workqueue, &whc->periodic_work);
313 * pzl_urb_dequeue - remove an URB (qset) from the periodic list
314 * @whc: the WHCI host controller
315 * @urb: the URB to dequeue
316 * @status: the current status of the URB
318 * URBs that do yet have qTDs can simply be removed from the software
319 * queue, otherwise the qset must be removed so the qTDs can be safely
322 int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
324 struct whc_urb *wurb = urb->hcpriv;
325 struct whc_qset *qset = wurb->qset;
326 struct whc_std *std, *t;
330 spin_lock_irqsave(&whc->lock, flags);
332 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
336 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
338 qset_free_std(whc, std);
340 std->qtd = NULL; /* so this std is re-added when the qset is */
343 pzl_qset_remove(whc, qset);
344 wurb->status = status;
345 wurb->is_async = false;
346 queue_work(whc->workqueue, &wurb->dequeue_work);
349 spin_unlock_irqrestore(&whc->lock, flags);
355 * pzl_qset_delete - delete a qset from the PZL
357 void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
360 queue_work(whc->workqueue, &whc->periodic_work);
361 qset_delete(whc, qset);
366 * pzl_init - initialize the periodic zone list
367 * @whc: the WHCI host controller
369 int pzl_init(struct whc *whc)
373 whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
374 &whc->pz_list_dma, GFP_KERNEL);
375 if (whc->pz_list == NULL)
378 /* Set T bit on all elements in PZL. */
379 for (i = 0; i < 16; i++)
380 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
382 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
388 * pzl_clean_up - free PZL resources
389 * @whc: the WHCI host controller
391 * The PZL is stopped and empty.
393 void pzl_clean_up(struct whc *whc)
396 dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,