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,
199 * pzl_update - request a PZL update and wait for the hardware to be synced
201 * @wusbcmd: WUSBCMD value to start the update.
203 * If the WUSB HC is inactive (i.e., the PZL is stopped) then the
204 * update must be skipped as the hardware may not respond to update
207 void pzl_update(struct whc *whc, uint32_t wusbcmd)
209 struct wusbhc *wusbhc = &whc->wusbhc;
211 mutex_lock(&wusbhc->mutex);
212 if (wusbhc->active) {
213 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
214 wait_event(whc->periodic_list_wq,
215 (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0);
217 mutex_unlock(&wusbhc->mutex);
220 static void update_pzl_hw_view(struct whc *whc)
222 struct whc_qset *qset, *t;
226 for (period = 0; period < 5; period++) {
227 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
228 whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
229 tmp_qh = qset->qset_dma;
230 qset->in_hw_list = true;
232 update_pzl_pointers(whc, period, tmp_qh);
237 * scan_periodic_work - scan the PZL for qsets to process.
239 * Process each qset in the PZL in turn and then signal the WHC that
240 * the PZL has been updated.
242 * Then start, stop or update the periodic schedule as required.
244 void scan_periodic_work(struct work_struct *work)
246 struct whc *whc = container_of(work, struct whc, periodic_work);
247 struct whc_qset *qset, *t;
248 enum whc_update update = 0;
251 spin_lock_irq(&whc->lock);
253 dump_pzl(whc, "before processing");
255 for (period = 4; period >= 0; period--) {
256 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
257 if (!qset->in_hw_list)
258 update |= WHC_UPDATE_ADDED;
259 update |= pzl_process_qset(whc, qset);
263 if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
264 update_pzl_hw_view(whc);
266 dump_pzl(whc, "after processing");
268 spin_unlock_irq(&whc->lock);
271 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
272 if (update & WHC_UPDATE_REMOVED)
273 wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
274 pzl_update(whc, wusbcmd);
278 * Now that the PZL is updated, complete the removal of any
281 spin_lock(&whc->lock);
283 list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
284 qset_remove_complete(whc, qset);
287 spin_unlock(&whc->lock);
291 * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
292 * @whc: the WHCI host controller
293 * @urb: the URB to enqueue
294 * @mem_flags: flags for any memory allocations
296 * The qset for the endpoint is obtained and the urb queued on to it.
298 * Work is scheduled to update the hardware's view of the PZL.
300 int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
302 struct whc_qset *qset;
306 spin_lock_irqsave(&whc->lock, flags);
308 qset = get_qset(whc, urb, GFP_ATOMIC);
312 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
314 usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
315 if (!qset->in_sw_list)
316 qset_insert_in_sw_list(whc, qset);
319 spin_unlock_irqrestore(&whc->lock, flags);
322 queue_work(whc->workqueue, &whc->periodic_work);
328 * pzl_urb_dequeue - remove an URB (qset) from the periodic list
329 * @whc: the WHCI host controller
330 * @urb: the URB to dequeue
331 * @status: the current status of the URB
333 * URBs that do yet have qTDs can simply be removed from the software
334 * queue, otherwise the qset must be removed so the qTDs can be safely
337 int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
339 struct whc_urb *wurb = urb->hcpriv;
340 struct whc_qset *qset = wurb->qset;
341 struct whc_std *std, *t;
345 spin_lock_irqsave(&whc->lock, flags);
347 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
351 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
353 qset_free_std(whc, std);
355 std->qtd = NULL; /* so this std is re-added when the qset is */
358 pzl_qset_remove(whc, qset);
359 wurb->status = status;
360 wurb->is_async = false;
361 queue_work(whc->workqueue, &wurb->dequeue_work);
364 spin_unlock_irqrestore(&whc->lock, flags);
370 * pzl_qset_delete - delete a qset from the PZL
372 void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
375 queue_work(whc->workqueue, &whc->periodic_work);
376 qset_delete(whc, qset);
381 * pzl_init - initialize the periodic zone list
382 * @whc: the WHCI host controller
384 int pzl_init(struct whc *whc)
388 whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
389 &whc->pz_list_dma, GFP_KERNEL);
390 if (whc->pz_list == NULL)
393 /* Set T bit on all elements in PZL. */
394 for (i = 0; i < 16; i++)
395 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
397 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
403 * pzl_clean_up - free PZL resources
404 * @whc: the WHCI host controller
406 * The PZL is stopped and empty.
408 void pzl_clean_up(struct whc *whc)
411 dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,